我是 React 的初学者,我正在研究如何一起使用 useReducer 和 useContext。 我正在处理的一个非常简单的研究示例只是一个名为 Greeting 的组件,它接受从 StateContext 传递的状态,它显示来自 Main.js 的用户 (Mike) 的姓名,并且它有一个按钮应该将动作分派到把名字改成约翰。 问题是当点击按钮时什么也没有发生,并且问候组件中的名称保持不变。 我注意到一旦操作 changeName 被调度,Greeting 组件中的 console.log 就不会被触发,这意味着,我相信,Greeting 组件没有被重新渲染。 为什么 Greeting 不重新渲染并显示更新的名称
这是主要组件:
import React, { useReducer } from 'react';
import { BrowserRouter, Switch, Route } from 'react-router-dom';
// components
import StateContext from './components/StateContext';
import DispatchContext from './components/DispatchContext';
import Greeting from './components/Greeting';
function reducer(state, action) {
switch (action.type) {
case 'changeName':
const newState = state;
newState.name = action.name;
return newState;
default:
return;
}
}
function App() {
const [state, dispatch] = useReducer(reducer, {
name: 'Mike',
});
console.log('name', state.name);
return (
<BrowserRouter>
<Switch>
<StateContext.Provider value={state}>
<DispatchContext.Provider value={dispatch}>
<Route path="/greeting">
<Greeting />
</Route>
</DispatchContext.Provider>
</StateContext.Provider>
</Switch>
</BrowserRouter>
);
}
export default App;
这是问候组件:
import React, { useContext } from 'react';
import StateContext from './StateContext';
import DispatchContext from './DispatchContext';
const Greeting = () => {
const state = useContext(StateContext);
const appDispatch = useContext(DispatchContext);
console.log('inside greeting component', state.name)
return (
<>
<div>{state.name}</div>
<button onClick={() => appDispatch({ type: 'changeName', name: 'John' })}>
Change Name
</button>
</>
);
};
export default Greeting;
DispatchContext:
import { createContext } from 'react';
const DispatchContext = createContext();
export default DispatchContext;
StateContext:
import { createContext } from 'react'
const StateContext = createContext()
export default StateContext
答案 0 :(得分:1)
这里的问题是状态对象突变。
function reducer(state, action) {
switch (action.type) {
case 'changeName':
const newState = state; // <-- newState is reference to state
newState.name = action.name; // <-- mutation!!
return newState; // <-- same state reference
default:
return;
}
}
当你改变那个状态对象并返回它时,它仍然是对前一个状态的引用,所以 React 会放弃重新渲染它。
返回一个新的状态对象。浅复制先前的状态到一个新的状态对象然后更新嵌套的属性。如果状态更新嵌套更深,那么您还需要浅复制任何更新的嵌套状态。不要忘记在默认情况下返回当前状态对象。
function reducer(state, action) {
switch (action.type) {
case 'changeName':
return {
...state,
name: action.name
};
default:
return state;
}
}