简体   繁体   English

React.js-可观察的状态,例如Ember.js中的可观察状态

[英]React.js - observable states like observable in Ember.js

In ember I could write something like this: 在余烬中,我可以这样写:

statusChanged(){
//...do some code
}.observes('observableProperty')

What is the right way to do the similar work in React.js? 在React.js中完成类似工作的正确方法是什么?

You can do your logic in component lifecycle methods componentWillMount , componentWillReceiveProps and others. 您可以在组件生命周期方法componentWillMountcomponentWillReceiveProps等中进行逻辑处理。 Whenever you pass new props to a component it will run throutg lifecycle. 每当您将新的道具传递给组件时,它将贯穿整个生命周期。 Read more about it 进一步了解

Alternatively you can use Rx.js . 或者,您可以使用Rx.js。 But conceptually I dont think it is a correct approach. 但是从概念上讲,我认为这不是正确的方法。 React implies you to manipulate props and states of a component and handle all logic in lifecycle or in synthetic events. React意味着您可以操纵组件的属性和状态,并处理生命周期或综合事件中的所有逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM