Upon page load, I see "hi2"
When I click the button, nothing happens. I tried with setUser
as well.
I suspect I'm just editing the props themselves and somehow the observable is not being triggered?
See sample code of it not working here in a brand new rails/react environment: https://github.com/bufordtaylor/mobxtest
======================
UPDATE:
I've reduced it to it's basic form, eliminating possible import errors, Provider errors, or constructor errors.
Here it is
import React from 'react'
import ReactDOM from 'react-dom'
import { observable, action, computed } from 'mobx';
import { Provider, inject, observer } from 'mobx-react';
class UserStore {
@action setUser(val) {
console.log(val);
this.user = val;
}
@observable user = "default";
}
const userStore = new UserStore();
@observer
class Hello extends React.Component {
render() {
return (
<div>
hi2 {this.props.userStore.user}
<button onClick={this.props.userStore.setUser.bind(this,"fwefwe")}>faew</button>
</div>
)
}
}
document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(
<Hello userStore={userStore} />,
document.getElementById('app'),
)
})
if you use the latest version of mobx, and babel version 7.12 add this to you constructor
makeObservable(this)
For anyone arriving here, make sure you're not being a complete idiot like me and that you didn't forget to add the @observer
decorator before the class component.
(Or the @observable
decorator in the store)
God, wasted a full day on that
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With