简体   繁体   English

如何在不重新加载整个列表的情况下在我的组件 CoinRow 中侦听此变量“编辑”的更改?

[英]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.

相关问题 如何在不刷新整个页面的情况下重新加载组件? - How to reload a component without refreshing the entire page? React:如何有效地制作我的组件,以便在道具更改时整个组件不会重新渲染? - React: How can I efficiently make my component so the entire component doesn't re-render when a prop changes? 如何在 Angular 中侦听组件中的服务变量更改 - How can I listen for a Service variable change in a component in Angular 我的 React 组件可以监听服务器中的变化并自行更新吗? - Can my React component listen to changes in the server and update itself? 当仅URL哈希值更改时,如何重新加载整个页面? - How can I reload the entire page when only the url hash changes? 我该如何听角度2中的代码更改? - how can I listen to changes in code in angular 2? 如何收听RactiveJS自定义组件? - How can I listen to a RactiveJS custom component? 当一个组件的状态更改时,如何防止整个列表重新呈现? - How do I prevent an entire list from re-rendering when state of one component changes? 如何在不重新加载页面的情况下向我的 url 添加参数? - How can I add a parameter to my url, without reload the page? 如何使用自定义历史记录 object 在我的主 App 组件中收听路由更改? - How can I listen to route change in my main App component, using a custom history object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM