ユーザーと場所は、各ユーザーが配置できる場所に2つの異なるデータベースとして用意されています。データを取得する正しい方法 - 反応
私はBasicInfo
コンポーネントを持っています。ユーザーの1箇所にロードする必要があります。私はハードコーディングしています今のところ、fetchPlaces
にユーザーIDを渡す必要があり
componentWillMount() {
this.props.dispatch(fetchUser())
this.props.dispatch(fetchPlaces(1))
}
BasicInfo
の
ComponentWillMount
、私はfetchUserの結果をどのように行うのですか? componentWillReceiveProps(nextProps)
でやりますか?それともそれ以外の方法がありますか?
componenetWillReceiveProps
が理にかなっていますが、イベントの連鎖であればどうしたらいいでしょうか?他のデータを取得する必要がある場合は、場所IDに応じている可能性があります。
アクション:あなたは、サンクまたは約束ミドルウェアを使用することができます
export function fetchPlaces(user_id) {
return function(dispatch) {
axios.get("/getPlaces?user_id=" + user_id)
.then((response) => {
console.log(response);
dispatch({type: "FETCH_PLACES_FULFILLED", payload: response.data})
})
.catch((err) => {
dispatch({type: "FETCH_PLACES_REJECTED", payload: err})
})
}
}
export function fetchPlace(place_id) {
return function(dispatch) {
axios.get("/getPlace?place_id=" + place_id)
.then((response) => {
console.log(response);
dispatch({type: "FETCH_PLACES_FULFILLED", payload: response.data})
})
.catch((err) => {
dispatch({type: "FETCH_PLACES_REJECTED", payload: err})
})
}
}
ありがとうJeff。だから私はComponentWillMountからデータを渡す方法はないと思う。 –
しかし、この場合、私はユーザー、場所、場所を取得する必要があるたびに? –
あなたの小道具はcomponentWillMount()で利用できます。たとえば、コンポーネントに「userId」プロパティがある場合、this.props.dispatch(fetchUser(this.props.userId))として渡すことができます。 –