私は反応するネイティブプロジェクトでaxios
とmobx
を使用しています。マウント時のコンポーネント(Home
)の1つは、すべてのAPIメソッドが編成されている別のファイルからメソッド(getBirds()
)を呼び出します。反応のクラスなしでmobx observerを使用する方法
store.js:
class BirdStore {
@observable birdList = [];
@action setBirdList = (birds) => {
this.birdList = birds;
};
}
Home.js:
@observer @inject("BirdStore")
export default class Home extends React.Component {
componentDidMount() {
api.getBirds()
...
}
}
api.js:
@inject('BirdStore') @observer
const api = {
getBirds() {
const url = website + '/api/birds/';
return axios.get(url)
.then((response) => {
this.props.BirdStore.setBirdList(response.data)
})
.catch((error) => {
console.log(error);
})
},
};
しかし、これは私にエラー与える:
をLeading decorator must be attached to a class declaration
getBirds()
から返されたサーバーのデータをHome
コンポーネントに使用してからsetBirdList()
というアクションを呼び出すことができますが、私はAPI関連のものを別に保存したいと考えていました。とにかくクラスなしでmobx
を使用するようになっているので、クラスコンポーネント以外のすべてのAPI関連の処理を行うことができますか?
'@inject( 'BirdStore')@ observer'は、あなたの' api'のような通常のオブジェクトではなく、Reactコンポーネントでのみ使うべきです。 'Home'でデコレータの順番を変更しようとします。 '@inject(" BirdStore ")@ observer' – Tholle
@Tholle Ok。ヘッドアップをありがとう! –