简体   繁体   English

如何调用在Reducer内部调用的Function,而上述Function使用useReducer返回的State?

[英]How to call a Function which is called inside Reducer, and the said Function uses the State returned by useReducer?

React v17反应 v17
I am using useReducer .我正在使用useReducer In the reducer function , I need to call another function check for network fetch.减速器 function中,我需要调用另一个 function检查网络获取。 In check( ) , I need to access the state , which is returned by useReducer .check()中,我需要访问由state返回的useReducer
I am getting this error can't access lexical declaration 'value' before initialization .我收到此错误can't access lexical declaration 'value' before initialization

const reducerFilterdValue = (state, action) => {
    let newState ;
    switch(action.type){
      case 'increment':
        if(check()===0){
          newState={...state,counter:state.counter+1}
        }        
      break;
      default:
        newState={...state}
      break;
    }    
    return newState;
    }

const check=()=>{
   return state.counter%2
}

const [state, dispatchValue] = useReducer(
  reducerFilterdValue,
  initialFilterdValue
) 

I have made aCodeSandBox for explanation purpose.我制作了一个CodeSandBox用于解释目的。

What is the efficient way to restructure the code?重构代码的有效方法是什么?

A. Should I need to call check from outside the useReducer ? A. 我是否需要从useReducer外部调用check To do that, I think I will need to use useEffect .为此,我想我需要使用useEffect

B. I declared check outside of the functional component as a let variable, and defined it after reducer function , but it was not working correctly and state was getting changed more than once. B. 我将功能组件之外的check声明为let变量,并在减速器 function之后定义它,但它无法正常工作,并且state被多次更改。

C. C。 Some better approach which I am unable to find out?一些我无法找到的更好的方法?

The reducer should be responsible for merging/reducing state and that is all.减速器应该负责合并/减少 state 仅此而已。

As someone in your question comments said, there should not be any side effectw in reducer (only state manipulations).正如您问题评论中的某人所说,减速器中不应该有任何副作用(仅 state 操作)。 So change the state in the reducer and check the counter state by using useEffect and dependency variables .因此,更改减速器中的 state 并使用useEffect依赖变量检查计数器state 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何添加类型以响应 useReducer 减速器 function? - how to add type to react useReducer reducer function? 尝试调用使用 state 变量的 function object 变量,该变量在 useEffect 中更改,并获得未定义的 state 变量 - try to call a function object which uses a state variable that is changed inside useEffect, and get undefined state variable 如何在钩子函数中调用reducer动作 - how to call the reducer action inside hook function 如何调用使用状态(由另一个函数设置)的函数并更新状态 - How to call a function that uses the state (which is set by another function) and updates state API调用期间如何在reducer函数中返回状态 - How to return state in reducer function during API call 当 reducer 函数依赖于一个组件 prop 时,传递给 useReducer 钩子的 reducer 函数会针对一次调度调用执行多次 - Reducer function passed to useReducer hook is executed multiple times for one dispatch call when reducer function is dependant on a component prop 如何在由 onSubmit 调用的函数内使用状态? - How to use state inside a function called by onSubmit? 我如何从另一个不是反应组件的文件中调用 useReducer 调度 function? (不使用 Redux) - How do i call a useReducer dispatch function from another file, which is not a react component? (without using Redux) 如何访问map内部函数内部的状态 - How to access state inside function which is inside of function inside of map React Hook “useReducer” 在 function “fetchData” 中被调用,它既不是 React function 组件也不是自定义 React Hook ZC1C425268E68385D1AB5074C17A9F - React Hook “useReducer” is called in function “fetchData” which is neither a React function component or a custom React Hook function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM