Reactjs 为什么即使草稿已更改,我的immer reducer仍不返回新值?

Reactjs 为什么即使草稿已更改,我的immer reducer仍不返回新值?,reactjs,immer.js,Reactjs,Immer.js,为什么即使减速机内部的吃水发生变化,我的伊默减速机仍不返回新值?我正在使用console.log检查我的草稿是否在immerReducer内更改: // inside my component that produces the immer function MyComponent() { const immerReducer = produce(reducer); const [state, dispatch] = useReducer(immerReducer, initialSta

为什么即使减速机内部的吃水发生变化,我的伊默减速机仍不返回新值?我正在使用console.log检查我的草稿是否在immerReducer内更改:

// inside my component that produces the immer
function MyComponent() {
  const immerReducer = produce(reducer);
  const [state, dispatch] = useReducer(immerReducer, initialState);
  const contextValue = useMemo(() => [state, dispatch], [state, dispatch]);
  const location = useLocation();

  useEffect(() => console.log("mounted"), []); // mounted twice

  useEffect(() => {
    dispatch({ type: ACTIONS.SET_MODE, pathname: location.pathname })
  }, [location.pathname]);

  console.log(state.mode); // I can se that this returns the "old" value, the initial value

  return (
  <MyContext.Provider value={contextValue}>
    {state.mode === MODES.DELETE ? <Deleted /> : <New />
  </MyContext.Provider>
  );
}

// inside the immer reducer file
export const reducer = (draft, action) => {
  switch (action.type) {
    case ACTIONS.SET_MODE:
      if (action.pathname.endsWith(MODES.DELETE)) {
        draft.mode = MODES.DELETE;
      } else if (action.pathname.endsWith(MODES.EDIT)) {
        draft.mode = MODES.EDIT;
      } else {
        draft.mode = MODES.NEW;
      }
      console.log("draft", draft.mode); // I can see that this logs out the wanted value
      return draft;
    ...
  }
}
//在生成immer的组件中
函数MyComponent(){
常数immerReducer=生产(减速机);
const[state,dispatch]=useReducer(immerReducer,initialState);
const contextValue=useMemo(()=>[state,dispatch],[state,dispatch]);
const location=useLocation();
useffect(()=>console.log(“装入”),[]);//装入两次
useffect(()=>{
分派({type:ACTIONS.SET_MODE,pathname:location.pathname})
},[location.pathname]);
console.log(state.mode);//我可以确定这将返回“old”值,即初始值
返回(
{state.mode===MODES.DELETE?:
);
}
//在immer reducer文件中
导出常量缩减器=(草稿、操作)=>{
开关(动作类型){
case ACTIONS.SET_模式:
if(action.pathname.endsWith(MODES.DELETE)){
draft.mode=MODES.DELETE;
}else if(action.pathname.endsWith(MODES.EDIT)){
draft.mode=MODES.EDIT;
}否则{
draft.mode=MODES.NEW;
}
console.log(“draft”,draft.mode);//我可以看到这会注销所需的值
退票;
...
}
}
,它似乎已更改,但当状态应已更新时,返回的值是旧值。这是否与导致初始状态的新装入有关?它看起来像是装入了两次。这是否与我在immer上拥有一个功能组件有关减速机


非常感谢您的帮助!

可能
生成
内部组件正在每个渲染上重新生成还原程序。请尝试将这些更改为

// inside my component that produces the immer
function MyComponent() {
  const immerReducer = produce(reducer); //<-- Remove it from here it is producing new reducer 
      
// inside the immer reducer file
export const reducer = (draft, action) => { //<-- Put produce here
  // export const reducer = produce(draft, action) => {} //<- Like this
      
    
//在生成immer的组件中
函数MyComponent(){

常量immerReducer=生产(减速机);//{/}//您应该在问题中添加更多的代码。目前,这根本不可理解。@tsfahmad我试图添加更多的代码。现在是否更可理解?您可以尝试使用钩子,看看是否有帮助。还可以尝试删除
useMemo
,至少暂时删除,看看这是否是问题所在。感谢您的输入!不幸的是,它没有解决问题解决我的问题