在react 监听数据对象的变更?

自己实现了一个感觉不太优雅, 主要在react中使用。
主要用于组件props的依赖。
如c组件的number,value 依赖a,b组件。a,b 组件变更时c组件也变更

window.watchData = [
    {key: 'a', value: 'a', number: 1},
    {key: 'c', value: {
        wathch: ['a.value', 'b.value'],
        handler: function(a, b){
            return a+b
        }
    }, number: {
        wathch: ['a.number', 'b.number'],
        handler: function(a, b){
            return a+b
        }
    }},
    {key: 'b', value: 'b', number: 2},
]
阅读 4.8k
2 个回答

请看 context

useEffect(() => {
    // Update the document title using the browser 
}, [item]);

Hook 文档

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题