我的应用程序已经发展到这样一个程度,即同一父母的孙子之间的可靠性达到一个混乱的程度 - 我需要状态管理,而且由于应用程序和团队规模相对较低的复杂性,MobX是您的选择(只有我)。 Redux似乎有点矫枉过正。
我正在尝试学习MobX,但对如何将其与Ajax调用集成有点困惑。我正在使用Axios从REST API获取数据。
我当前的组件结构是分层的:
<Container /> // Dummy component
<Child 1 /> <Child 2 /> // Ajax calls exist in both
<Grandchild 1.1 /> <Grandchild 1.1 /> // There are ~10 for each
我在{1}}中使axios
获取请求,在componentDidMount
中调用它,并将数据传播给具有单向数据流的子项,就像通常在反应。
假设我的<Child 1/>
组件现在看起来像这样:
class Child1 extends React.Component {
// Set initial state etc.
loadData = () => {
let that = this;
axios.get("/api/child1/").then(function(response){
that.setState({data: response.data });
}).catch(function(error){
// Handles error
})
};
componentDidMount () {
this.loadData();
}
// Continues ..
}
结构中的axios.get
现在应该切换到MobX,如何从Child1
访问它?
假设我有不同的axios.get
次调用,它们是否应该位于单个store.js
文件中并由this.props
调用?