【发布时间】:2021-07-06 14:18:44
【问题描述】:
在组件的函数(渲染阶段)中定义reducer函数是否有效且安全?
const Component = () => {
// is this okay to be here or should be outsite the function?
const reducer = (state, action) => {
// do stuff
}
const [state, dispatch] = React.useReducer(reducer, initState);
return (
<div />
)
}
我想这样做的原因是因为我需要访问组件的道具。如果这种用法不正确,如何访问 reducer 中的 props?
【问题讨论】:
标签: javascript reactjs use-reducer