Reactjs 如果我在useEffect中调用多个useStates,它们会被批处理还是会导致性能问题?
如果我在useEffect中调用多个useStates,它们会被批处理还是会导致性能问题 例如:Reactjs 如果我在useEffect中调用多个useStates,它们会被批处理还是会导致性能问题?,reactjs,react-hooks,use-effect,Reactjs,React Hooks,Use Effect,如果我在useEffect中调用多个useStates,它们会被批处理还是会导致性能问题 例如: useEffect( () => { setDate(date) setStartDate(startDate) setEndDate(endDate) setSelectedNodes([]) setName('Test') }, [dateChanged, startDate, endDate]
useEffect(
() => {
setDate(date)
setStartDate(startDate)
setEndDate(endDate)
setSelectedNodes([])
setName('Test')
},
[dateChanged, startDate, endDate]
)
没有什么内在的错误,但我发现,这使它更易于管理
const initialState = {count: 0};
function reducer(state, action) {
switch (action.type) {
case 'specialThing':
return {endDate: action.endDate, startDate: action.startDate, ...etc}
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
default:
throw new Error();
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
useEffect(() => {
dispatch({ type: 'specialThing', startDate, endDate, ...etc })
}, [dateChanged, startDate, endDate])
return (
...
);
}
按照计划,它们按顺序运行。