React Hooks之useReducer
2018-12-28 16:41
344 查看
useReducer :接受类型为 (state, action) => newState 的reducer,并返回与 dispatch 方法配对的当前状态。(如果你熟悉Redux,你已经知道它是如何工作的。)
下面定义两个计数器
Counter组件和
CounterTest组件,使用 useReducer 使它们
共享一个count state,当一个组件的 count state发生变化时,另一个组件的count state也会相应的改变。
- reducer.js
import { useReducer } from "react"; const initialState = 0; function reducer(state, action) { switch (action.type) { case "reset": return initialState; case "increment": return { count: state.count + 1 }; case "decrement": return { count: state.count - 1 }; default: // A reducer must always return a valid state. // Alternatively you can throw an error if an invalid action is dispatched. return state; } } export default reducer;
- Counter.js
import React from "react"; function Counter(props) { return ( <div> Count: {props.state.count} <button onClick={() => props.dispatch({ type: "reset" })}>Reset</button> <button onClick={() => props.dispatch({ type: "increment" })}>+</button> <button onClick={() => props.dispatch({ type: "decrement" })}>-</button> </div> ); } export default Counter;
- CounterTest.js
import React from "react"; function CounterTest(props) { return ( <div> Count: {props.state.count} <button onClick={() => props.dispatch({ type: "reset" })}>Reset</button> <button onClick={() => props.dispatch({ type: "increment" })}>+</button> <button onClick={() => props.dispatch({ type: "decrement" })}>-</button> </div> ); } export default CounterTest;
- index.js
import React, { useReducer } from "react"; import reducer from "./reducer"; import Counter from "./Counter"; import CounterTest from "./CounterTest"; const App = () => { const [state, dispatch] = useReducer(reducer, { count: 0 }); return ( <div className="App"> <Counter state={state} dispatch={dispatch} /> <CounterTest state={state} dispatch={dispatch} /> </div> ); }; const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement);
ps:此处还有两种方法对代码进行优化:
方法一:将共同的状态 state 和 dispatch 通过React.createContext存入到context中,再通过useContext API获取context中的内容。
方法二:直接采用React Context API。
接下来会对这两种方法分别进行说明。
相关文章推荐
- 【计数器改写方法一】 React Hooks之useContext
- Item 33: Use the new Modifier Only to React to Base Class Updates(Effective C#)
- React Native Reducer结构较深与render map展示刷新问题
- React Native use IconFont
- [React] Use a Render Porp
- 在 React 和 Vue 中尝鲜 Hooks
- 关于react-router中<Link>的调试: You should not use <Link> outside a <Router>
- [React] Use the new React Context API
- react-redux的使用从action规划到reducer实现及完整案例
- 在 React 和 Vue 中尝鲜 Hooks
- DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead解决办法
- react开发报错Use the prop-types package from npm instead.
- React 16.8 发布,Hooks的稳定实施
- Tapable.plugin is deprecated. Use new API on `.hooks` instead
- [React] Use React Context to Manage Application State Through Routes
- [React Native] Use the SafeAreaView Component in React Native for iPhone X Compatibility
- react native use map function
- [React] Use the URL as the source of truth in React
- ReactNative.createElement is deprecated. Use React.createElement from the "react" package instead.
- React 使用Hooks简化受控组件的状态绑定