Simply rough, instead of state in MOBX, use @observable to introduce observable attributes __react

Source: Internet
Author: User
Http://cn.mobx.js.org/refguide/observer-component.html
Observable Local component states

Like ordinary classes, you can introduce observable properties on the react component by using the @observable adorner. This means that you can have equally powerful local states in your components, without the need to manage them through react's lengthy and mandatory setstate mechanisms. The response state is invoked by render, but no other react lifecycle method is invoked, except for Componentwillupdate and Componentdidupdate. If you need to use other react lifecycle methods, simply use the general react API based on the state.

The above example can also be written like this:

Import {Observer} from "Mobx-react"
Import {observable} from ' Mobx '

@observer class Timer extends React.component {
    @observable secondspassed = 0
  
   componentwillmount () {
        setinterval (() => {
            this.secondspassed++
        }, 1000)
    }

    render () {
        Return (<span>seconds passed: {this.secondspassed} </span>)
    }

Reactdom.render (< Timer/>, document.body)
Copy
  

For a more advantageous use of observable local component state, see why I no longer use the SetState three reasons.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.