2017-01-06 13 views
0

ユーザーと場所は、各ユーザーが配置できる場所に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}) 
 
     }) 
 
    } 
 
}

答えて

1

すでにredux-thunkが使用されているようですか?もしそうなら、あなたは(あなたのaxios.getから約束を返すことができます)を呼び出します:

return axios.get(...); 

...あなたは、この(私はあなたのfetchUseruserがどのように見えるかを推測している)のような何かを行うことができます

this.props.dispatch(fetchUser()).then(user => this.props.dispatch(fetchPlaces(user.id))) 
+0

ありがとうJeff。だから私はComponentWillMountからデータを渡す方法はないと思う。 –

+0

しかし、この場合、私はユーザー、場所、場所を取得する必要があるたびに? –

+1

あなたの小道具はcomponentWillMount()で利用できます。たとえば、コンポーネントに「userId」プロパティがある場合、this.props.dispatch(fetchUser(this.props.userId))として渡すことができます。 –

1

。あなたは動機と例をdocumentationで見つけることができます。

+0

ありがとうございます。 Haluk –

関連する問題