user2923418 user2923418 - 1 year ago 96
React JSX Question

What is the Mobx way to do handle set state with callbacks

I am trying to convert an existing application that uses set state to use Mobx. How can we i achieve the set state callback functionality using mobx. Do i need to use componentDidUpdate to achieve this functionality?

Answer Source

setState in React is asynchronous. Modifying an observable in MobX is synchronous, so there is no need for a callback:

class Example extends Component {
  @observable clicks = 0;

  render() {
    return <button onClick={this.handleClick}> click me </button>;

  handleClick = () => {
    // The component has now re-rendered!
    // ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download