Answer the question
In order to leave comments, you need to log in
How to work with mobx?
There is store.js:
import { observable, action } from 'mobx';
import axios from 'axios';
class Store {
@observable users = null;
@action
getUsers() {
axios.get('https://jsonplaceholder.typicode.com')
.then((response) => {
this.categories = response.data;
})
.catch(error => console.error(error));
}
}
export default new Store();
import React, { Component } from 'react';
import Form from './components/Form';
class App extends Component {
render() {
return (
<Form />
);
}
}
export default App;
import React from 'react';
import ReactDOM from 'react-dom';
import store from './store';
import App from './App';
ReactDOM.render(<App store={store} />, document.getElementById('root'));
import React, { Component } from 'react';
class Form extends Component {
render() {
return (
<form className="form">
<select>
<option></option>
</select>
</form>
);
}
}
export default Form;
Answer the question
In order to leave comments, you need to log in
Answer 1:
import { Provider } from "mobx-react";
ReactDOM.render(<Provider store={store}>
<App />
</Provider>, document.getElementById('root'));
import { inject } from "mobx-react";
import React, { Component } from 'react';
import { inject } from "mobx-react";
@inject("store")
class Form extends Component {
render() {
return (
<form className="form">
<select>
<option></option>
</select>
</form>
);
}
}
export default Form;
import React from "react";
import { inject } from "mobx-react";
const App = () => {
return <div className="App">Hello World!</div>;
};
export default inject("store")(App);
<button onClick={() => this.props.store.getUsers()}></button>
And, as far as I understand, you need to wrap the called function in an arrow function, that is: () =>
if the desired method is outside the current file. In your case, yes. const { getUsers } = this.props.store;
// какой-то код
return <button onClick={() => getUsers()}></button>;
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question