[英]How can I listen for changes to this variable 'editing' in my component CoinRow without reload the entire list?
i have list rendering a rowComponent, until here everything is working, like this:我有一个渲染 rowComponent 的列表,直到这里一切正常,如下所示:
const [editing, setEditing] = useState(false);
nav.setOptions({
headerLeft: () => <Button
title='Edit'
onPress={() => {
setEditing(!editing)
>
</Button>
<MyList>
<CoinRow editing={editing}></CoinRow>
</MyList>
i have a left button for change the state of editing with setEditing我有一个左按钮,用于使用 setEditing 更改编辑的 state
so now i need to How can I listen for changes to this variable in this component?所以现在我需要如何在这个组件中监听这个变量的变化?
const CoinRow = ({ editing }) => {
console.log('editing', editing)
but never i see that console, i dont want to reload all the rows但我从来没有看到那个控制台,我不想重新加载所有行
If you want to listen to an event on a specific props (in your case the update of state), you need to use the useEffect
hook.如果您想监听特定道具上的事件(在您的情况下是状态更新),您需要使用useEffect
挂钩。
const [editing, setEditing] = useState(false);
useEffect(() => {
//The code that need to fire when "editing" is update
...
//By putting "editing" into this array, useEffect will listen on update on "editing"
}, [editing])
Note that useEffect
render at every first render of a component.请注意, useEffect
在组件的每次第一次渲染时渲染。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.