我设置了这个 reducer.js 文件来使用 React 的 useReducer
https://reactjs.org/docs/hooks-reference.html#usereducer
import {useReducer} from 'react';
const initialState = {
test: 0,
};
const reducer = (state, action) => {
switch (action.type) {
case 'reset':
return initialState;
case 'addTest':
return {test: state.test + 1};
case 'removeTest':
return {test: state.test - 1};
}
};
export const getReducer = () => {
return useReducer(reducer, initialState);
};
getReducer
现在我可以通过不同的渲染函数获取状态和调度:
import React from 'react';
import ReactDOM from 'react-dom';
import {getReducer} from './reducer';
const Button = (props) => (
<button
type="button"
onClick={() => props.dispatch({type: props.type})}>
{props.children}
</button>
);
const App = () => {
const [state, dispatch] = getReducer();
return (
<React.Fragment>
{state.test}
<Button dispatch={dispatch} type="addTest">Add 1</Button>
<Button dispatch={dispatch} type="removeTest">Remove 1</Button>
<Button dispatch={dispatch} type="reset">Reset</Button>
</React.Fragment>
);
};
ReactDOM.render(<App />, document.getElementById('root'));
传递dispatch
函数并让其他组件调用 props.dispatch 确实感觉很奇怪。有没有更干净的方法来设置它?
如果你想尝试不同的模式,我在这里设置了一个 repo: https ://github.com/dancrew32/hooks