Skip to content

Commit

Permalink
Use useSelector the right way
Browse files Browse the repository at this point in the history
  • Loading branch information
kale-stew committed May 27, 2020
1 parent 3ebe93d commit ffeeaf0
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions test/typescript/sample-react-redux-usage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// This is compiled using `tsc` in our `test-ts-usage` script
import React from 'react';
import ReactDOM from 'react-dom';
// useSelector
import { Provider, useSelector, DefaultRootState } from 'react-redux';
import { Provider, useSelector } from 'react-redux';
import { createStore } from 'redux';

import equal from '../../index.js';
Expand All @@ -23,11 +22,11 @@ const testArr: IItem[] = [
{ text: 'air', id: '8' },
];

interface IContainerState extends DefaultRootState {
overlap: IItem[];
}
type IContainerState = {
overlap: () => IItem[];
};

const overlap = (state = [], action) => {
const overlap = (state: IItem[] = [], action) => {
switch (action.type) {
case 'ADD_ITEM':
return state.concat([action.text]);
Expand All @@ -36,19 +35,21 @@ const overlap = (state = [], action) => {
}
};

const store = createStore(overlap, ['mountain']);
const store = createStore(overlap, [{ text: 'mountain', id: '11' }]);

class TestContainer extends React.Component<{}, IContainerState> {
render() {
useSelector(this.state, equal);

// useSelector compares arg1 to the redux state store,
// using arg2's equality fn
// return value will be the same as the equality fn
return (
<div>
Testing react-redux
<div>
{testArr.map((item) => (
<p key={item.id}>{item.text}</p>
))}
{testArr.map((item) => {
useSelector(item, equal);
return <p key={item.id}>{item.text}</p>;
})}
</div>
</div>
);
Expand Down

0 comments on commit ffeeaf0

Please sign in to comment.