-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(add): react-redux hooks version
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<div id="app"></div> | ||
<script src="script.tsx"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |