gpt4 book ai didi

javascript - 如何在 React 中访问 child 的状态? ( react 钩子(Hook))

转载 作者:搜寻专家 更新时间:2023-11-01 04:34:38 25 4
gpt4 key购买 nike

使用 react-hooks 获取子状态的最佳解决方案是什么?

我尝试过各种方法。在我最后得到的那个下面。

表单(父级)


export const Form: FunctionComponent<IProps> = ({ onFinish, initState }) => {
const formInputsStateRef = useRef({})

const handleFinish = () => {
const params = formInputsStateRef.current
console.log(params)
onFinish(params)
}

return (
<div>
<Inputs initState={initState} stateRef={formInputsStateRef} />
<S.Button onClick={handleFinish}>
Finish
</S.Button>
</div>
)
}

输入( child )

export const Inputs: FunctionComponent<IProps> = ({ initState, stateRef }) => {
const [pool, setPool] = useState(initState.pool)
const [solarPanel, setSolarPanel] = useState(initState.solarPanel)

useEffect(() => {
stateRef.current = { pool, solarPanel }
})

const handlePoolInput = () => {
setPool('new pool')
}

const handleSolarPanelInput = () => {
setSolarPanel('new solar panel')
}

return (
<div>
<h2>{pool}</h2>
<S.Button onClick={handlePoolInput}>Change pool</S.Button>
<h2>{solarPanel}</h2>
<S.Button onClick={handleSolarPanelInput}>Change solar panel</S.Button>
<h2>-----</h2>
</div>
)
}

它以这种方式工作,但我不喜欢它在每次渲染时创建一个对象这一事实。


输入( child )

useEffect(() => {
stateRef.current = { pool, solarPanel }
})

最佳答案

您可以将 pool 和 solarPanel 作为第二个参数传递给 useEffect,以便仅在这些值发生变化时更新状态以引用

export const Inputs: FunctionComponent<IProps> = ({ initState, stateRef }) => {
const [pool, setPool] = useState(initState.pool)
const [solarPanel, setSolarPanel] = useState(initState.solarPanel)

useEffect(() => {
stateRef.current = { pool, solarPanel }
}, [pool, solarPanel])

const handlePoolInput = () => {
setPool('new pool')
}

const handleSolarPanelInput = () => {
setSolarPanel('new solar panel')
}

return (
<div>
<h2>{pool}</h2>
<S.Button onClick={handlePoolInput}>Change pool</S.Button>
<h2>{solarPanel}</h2>
<S.Button onClick={handleSolarPanelInput}>Change solar panel</S.Button>
<h2>-----</h2>
</div>
)
}

但是,要使用 ref 更好地控制子值的句柄,您可以使用 useImperativeHandle Hook 。

child

const InputsChild: FunctionComponent<IProps> = ({ initState, ref }) => {
const [pool, setPool] = useState(initState.pool)
const [solarPanel, setSolarPanel] = useState(initState.solarPanel)

useImperativeHandle(ref, () => ({
pool,
solarPanel
}), [pool, solarPanel])

const handlePoolInput = () => {
setPool('new pool')
}

const handleSolarPanelInput = () => {
setSolarPanel('new solar panel')
}

return (
<div>
<h2>{pool}</h2>
<S.Button onClick={handlePoolInput}>Change pool</S.Button>
<h2>{solarPanel}</h2>
<S.Button onClick={handleSolarPanelInput}>Change solar panel</S.Button>
<h2>-----</h2>
</div>
)
}

export const Inputs = forwardRef(InputsChild);

父级

export const Form: FunctionComponent<IProps> = ({ onFinish, initState }) => {
const formInputsStateRef = useRef({})

const handleFinish = () => {
const params = formInputsStateRef.current
console.log(params)
onFinish(params)
}

return (
<div>
<Inputs initState={initState} ref={formInputsStateRef} />
<S.Button onClick={handleFinish}>
Finish
</S.Button>
</div>
)
}

关于javascript - 如何在 React 中访问 child 的状态? ( react 钩子(Hook)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54702565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com