0
私はReactを学んでいます。私はこれが非常に単純な仕事だと思ったが、私は腋生の約束が完了した後、店舗での発送を諦めてしまった。Axiosでストアディスパッチを発行する方法
これは私のクラス全体のソースコードです。
約束の中で、店の価値は未定義です。私はたくさんの組み合わせを試しました。 Angularでは、私はちょうどクラス変数を定義し、それを使うことができます。私はここで何をすべきですか?
class CricketScorer extends Component {
constructor(props, context) {
super(props, context);
const { store } = this.context;
this.isLoading = false;
}
componentDidMount() {
console.log('Get Initial State from Server');
axios.get(jsonURL)
.then((response) => {
console.log('axios.get SUCCESS', response.data);
console.log('componentDidMount Value Of Store', this.store);
this.store.dispatch({
type: constants.INITIAL_STATE,
payload: response.data
});
})
.catch(error => {
console.log('axios.get ERROR', error);
});
}
render() {
console.log('Cricketscorer render()', this.store)
// This function always recieve single argument [this.props]
// All other passed values are as "this.props.recipes"
return (
<div>
<ScoreBoard />
<ScoreAction />
{this.isLoading === true && <span>Loading...</span>}
</div>
);
}
}
コンソール出力は次のとおりです。
Navigated to http://localhost:3000/
Cricketscorer render() undefined
Scoreboard state Object {matchId: 0, thisOver: Array(0), prevOver: Array(0), oversTotal: 0, teamBatting: Object…}
Get Initial State from Server
axios.get SUCCESS Object {matchId: 100, thisOver: Array(0), prevOver: Array(0), oversTotal: 0, teamBatting: Object…}
componentDidMount Value Of Store undefined
axios.get ERROR TypeError: Cannot read property 'dispatch' of undefined
あなたは、私が派遣を発行しようとしています、それは言ってそこに私にエラーを与えている見ることができるように:
axios.get ERROR TypeError: Cannot read property 'dispatch' of undefined
可能な重複して同じよう
どこでもあなたのコンポーネントでと
this.store
と後でアクセスすることのようにそれを割り当てることができますしかし[「この」キーワードの作業を行う方法は?] (https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) –
this.context.store.dispatch?あなたの店は文脈から来ていませんか? – Amida
@Amidaありがとうございました。しかし、this.context.storeをいくつかの変数に代入し、それをコンポーネント全体で使用する方法があります。 – Sallu