Skip to content
This repository was archived by the owner on Dec 31, 2020. It is now read-only.
/ mobx-react Public archive
This repository was archived by the owner on Dec 31, 2020. It is now read-only.

Calling actions asynchronously from componentWillMount does not trigger react rerender, while from componentDidMount does #195

@philipstanislaus

Description

@philipstanislaus

Hello,

thanks for your awesome work on mobx-react!

I observe a behavior that I could not find documented and looks like a bug: when calling an action asynchronously from a componentWillMount hook, the react component will not rerender, when calling the action from an componentDidMount hook, it will.

Failing example with componentWillMount

import { action, observable } from 'mobx'
import { observer } from 'mobx-react'
import * as React from 'react'

@observer
export class WillMount extends React.Component<{}, {}> {
  @observable counter = 0

  @action inc = () => this.counter++

  componentWillMount = () => {
    setTimeout(() => this.inc(), 3000)
  }

  render() {
    return <p>{this.counter}<button onClick={this.inc}>+</button></p>
  }
}

As the following console output shows, this.inc() is triggered, and this.counter is updated correctly, yet no rerender is scheduled:
bildschirmfoto 2017-01-17 um 12 13 44

Strangely, calling the action manually by clicking the button does also not trigger a rerender:
bildschirmfoto 2017-01-17 um 12 15 45

Working example with componentDidMount

import { action, observable } from 'mobx'
import { observer } from 'mobx-react'
import * as React from 'react'

@observer
export class DidMount extends React.Component<{}, {}> {
  @observable counter = 0

  @action inc = () => this.counter++

  componentDidMount = () => {
    setTimeout(() => this.inc(), 3000)
  }

  render() {
    return <p>{this.counter}<button onClick={this.inc}>+</button></p>
  }
}

This works as expected, here is the console.log:
bildschirmfoto 2017-01-17 um 12 17 16

Also clicking the button works now, see:
bildschirmfoto 2017-01-17 um 12 17 36

Questions

Is this expected behaviour? If yes, can we enhance the documentation?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions