Skip to content

Commit

Permalink
feat(add): react-redux hooks version
Browse files Browse the repository at this point in the history
  • Loading branch information
seognil committed Jul 10, 2020
1 parent 5bf58a1 commit 623a5f0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
15 changes: 15 additions & 0 deletions react-redux/hooks-only/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-redux": "^7.2.0",
"redux": "^4.0.5"
},
"devDependencies": {
"@types/node": "^14.0.20",
"@types/react": "^16.9.41",
"@types/react-dom": "^16.9.8",
"@types/react-redux": "^7.1.9",
"typescript": "^3.9.6"
}
}
2 changes: 2 additions & 0 deletions react-redux/hooks-only/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div id="app"></div>
<script src="script.tsx"></script>
47 changes: 47 additions & 0 deletions react-redux/hooks-only/src/script.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as React from 'react';
import ReactDOM from 'react-dom';
import { createStore, Reducer } from 'redux';
import { Provider, useSelector, useDispatch, useStore } from 'react-redux';

// * ------------------------------------------------

type MyState = { val: number };

// * ----------------

const reducer: Reducer<MyState, { type: string; payload: number }> = (
state = { val: 0 },
action,
) => ({ val: state.val + (action.payload ?? 0) });
const store = createStore(reducer);

// * ------------------------------------------------

const App: React.FC = () => {
return (
<Provider store={store}>
<Comp />
</Provider>
);
};

// * ----------------

const Comp: React.FC = () => {
const store = useStore<MyState>();
const val = useSelector<MyState, number>((state) => state.val);
const dispatch = useDispatch();

return (
<div>
<h3>Hello react-redux</h3>
<button onClick={() => dispatch({ type: 'any', payload: 1 })}> add 1 </button>
<p>useSelector mapped: {val}</p>
<p>store.getState().val: {store.getState().val} </p>
</div>
);
};

// * ------------------------------------------------

ReactDOM.render(<App />, document.querySelector('#app'));

0 comments on commit 623a5f0

Please sign in to comment.