I would like to pass only one observable of StoreA to StoreB without passing the full StoreA.
Here is an example, where I pass the user observable in the constructor of TodoStore from the UserStore, but obviously it doesn't update when I change the observable.
So the question is how to do it the cleanest and shortest way, without passing the full reference of the UserStore?
import * as React from "react";
import { render } from "react-dom";
import { observer } from "mobx-react";
import { observable, computed, action } from "mobx";
export class TodoStore {
@observable public todos: string[] = [ "default" ];
private user:string;
constructor(user:string) {
this.user = user;
}
@action.bound
public addTodo = (todo: string) => {
this.todos.push(this.user + ": " + todo);
};
}
export class UserStore {
@observable public user: string = "zubi";
@action.bound
public change = (name: string) => {
this.user = name;
};
}
var userStore = new UserStore();
var todoStore = new TodoStore(userStore.user);
@observer
class App extends React.PureComponent {
public render() {
return (
<div>
<button onClick={e => {todoStore.addTodo("hello" + Math.floor(Math.random() * 1000));}}>
add message
</button>
<button onClick={e => { userStore.change("Eve" + Math.floor(Math.random() * 100),);}}>
change name
</button>
<div>
{todoStore.todos.map(todo => { return <div key={todo}>{todo}</div>;})}
</div>
</div>
);
}
}
render(<App />, document.getElementById("root"));
Here you can check the full code: https://codesandbox.io/s/mobx-test-zubi-pf1qn
Thanks a lot!