Javascript 设置React useReducer的最佳方法

Javascript 设置React useReducer的最佳方法,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,我将这个reducer.js文件设置为使用React的useReducer 现在我可以在不同的渲染函数中通过getReducer获取状态和分派: import React from 'react'; import ReactDOM from 'react-dom'; import {getReducer} from './reducer'; const Button = (props) => ( <button type="button" onClick={

我将这个reducer.js文件设置为使用React的useReducer

现在我可以在不同的渲染函数中通过
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'));
从“React”导入React;
从“react dom”导入react dom;
从“/reducer”导入{getReducer};
常量按钮=(道具)=>(
props.dispatch({type:props.type})}>
{props.children}
);
常量应用=()=>{
const[state,dispatch]=getReducer();
返回(
{state.test}
加1
删除1
重置
);
};

ReactDOM.render(

如何定义动作并将它们映射到减速器

const mapDispatch => dispatch => ({
  reset: () => dispatch({ type: 'reset' }),
  addTest: () => dispatch({ type: 'addTest' }),
  removeTest: () => dispatch({ type: 'removeTest' })
})

const Button = (props) => (
  <button
    type="button"
    onClick={props.onClick}>
    {props.children}
  </button>
);

const App = () => {
  const [state, dispatch] = getReducer();
  const actions = mapDispatch(dispatch)
  return (
    <React.Fragment>
      {state.test}
      <Button onClick={actions.addTest}>Add 1</Button>
      <Button onClick={actions.removeTest}>Remove 1</Button>
      <Button onClick={actions.reset}>Reset</Button>
    </React.Fragment>
  );
};
const-mapspatch=>dispatch=>({
重置:()=>分派({type:'reset'}),
addTest:()=>dispatch({type:'addTest'}),
removeTest:()=>分派({type:'removeTest'})
})
常量按钮=(道具)=>(
{props.children}
);
常量应用=()=>{
const[state,dispatch]=getReducer();
const actions=mapDispatch(调度)
返回(
{state.test}
加1
删除1
重置
);
};

这里没有什么新东西;只是模仿
react redux
的工作方式。

如何定义您的操作并将其映射到您的应用程序

const mapDispatch => dispatch => ({
  reset: () => dispatch({ type: 'reset' }),
  addTest: () => dispatch({ type: 'addTest' }),
  removeTest: () => dispatch({ type: 'removeTest' })
})

const Button = (props) => (
  <button
    type="button"
    onClick={props.onClick}>
    {props.children}
  </button>
);

const App = () => {
  const [state, dispatch] = getReducer();
  const actions = mapDispatch(dispatch)
  return (
    <React.Fragment>
      {state.test}
      <Button onClick={actions.addTest}>Add 1</Button>
      <Button onClick={actions.removeTest}>Remove 1</Button>
      <Button onClick={actions.reset}>Reset</Button>
    </React.Fragment>
  );
};
const-mapspatch=>dispatch=>({
重置:()=>分派({type:'reset'}),
addTest:()=>dispatch({type:'addTest'}),
removeTest:()=>分派({type:'removeTest'})
})
常量按钮=(道具)=>(
{props.children}
);
常量应用=()=>{
const[state,dispatch]=getReducer();
const actions=mapDispatch(调度)
返回(
{state.test}
加1
删除1
重置
);
};

这里没有什么新东西;只是模仿
react redux
的工作方式。

您可以在树的任何级别的组件中调用
getReducer
,以获得
dispatch
函数,您不需要将其传递下去。在您的示例中
dispatch={dispatch}
不是一个好主意,而是从这里调用
dispatch
,或者使用
getReducer
,从
按钮中获取
dispatch
函数。这至少是我的理解。你可以在树的任何级别的组件中调用
getReducer
来获取
dispatch
函数,你不需要在您的示例中,
dispatch={dispatch}
不是一个好主意,而是从这里调用
dispatch
,或者使用
getReducer
按钮中获取
dispatch
函数。这至少是我的理解。是什么让getReducer可以访问?是什么让getReducer可以访问?