Mobx: Reaction does not converge to a stable state because NaN != NaN

Created on 29 Nov 2016  路  4Comments  路  Source: mobxjs/mobx

mobx 2.6.3.

I have a fairly complex chain of dependent values that can be simplified to this:

@observer
class Tmp extends React.Component<{}, {}> {
    @observable
    someValue = 0;

    @action updateSomething() {
        // ...
        this.someValue = NaN;
    }

    render() {
        this.updateSomething();
        return <div>value is {this.someValue}</div>
    }
}
const tmp = ReactDOM.render(<Tmp />, document.getElementById("root"));

setTimeout(action(() => tmp.someValue = 100), 1000);

The render function is called forever because it keeps trying to set someValue to NaN and thinks it has changed again. It works if updateSomething sets someValue to anything other than NaN. My guess is that this happens because NaN !== NaN so the check if the value is different would need to be before === after || (isNaN(before) && isNaN(after))

2016-11-29-21-20-12

Most helpful comment

As a temporarily work around, you could do that check yourself before assigning,

The fix for my problem was actually somewhere else; the value should never have become NaN in the first place; but the infinite loop was unexpected ;)

me hating javascript now

Well, to be fair this is how it works in any language that follows the IEEE standard; e.g. https://github.com/dmlloyd/openjdk/blob/jdk9/jdk9/jdk/src/java.base/share/classes/java/lang/Double.java#L550

All 4 comments

Nice find. You are most probably right.

_me hating javascript now_

As a temporarily work around, you could do that check yourself before assigning, but thanks for reporting!

As a temporarily work around, you could do that check yourself before assigning,

The fix for my problem was actually somewhere else; the value should never have become NaN in the first place; but the infinite loop was unexpected ;)

me hating javascript now

Well, to be fair this is how it works in any language that follows the IEEE standard; e.g. https://github.com/dmlloyd/openjdk/blob/jdk9/jdk9/jdk/src/java.base/share/classes/java/lang/Double.java#L550

Since this is strictly speaking "correct", this could be considered not to be a bug... not sure lol. Closing for now. Any body running into this in the future, or having a strong argument that it should be changed, feel free to reopen :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mweststrate picture mweststrate  路  35Comments

winterbe picture winterbe  路  34Comments

Nopik picture Nopik  路  33Comments

FouMez picture FouMez  路  31Comments

jefffriesen picture jefffriesen  路  29Comments