別のコンポーネントにあるAxios reqのデータを別のコンポーネントに渡したいと思います。私は私のコードスニペットで詳しく説明します。React - Axios reqから別のコンポーネントにデータを渡す
私のdoorsLayout.js私は、アドレスへのAxios呼び出しを行っています。そして、私はresのデータを取得しています。その後
this.serverRequest = axios
.get('http://blablabla/locks')
.then(res => {
// Rerender state
this.setState({
res,
dataToDisplay: res.data.slice(0, numToDisplay || 5)
})
})
私はと呼ばれる新しいコンポーネントを作っています(同じファイル内)doorsItem.jsここ:
const items = dataToDisplay.map(item => <DoorsItem item={item} />)
そしてDoorsItemの内側に私はAxios呼び出しからデータを出力しています:
const DoorsItem = ({ item }) =>
<Grid>
<Row key={item._id} className="show-grid">
<Col md={12}>
<ul style={{ marginTop: '10px' }}>
<li className="info-container">
<h4 className="title-text-container">{item.address.street}</h4>
{/*etc etc*/}
私はでAxiosコールから得たすべての情報を出力できます。しかし同じファイルでは、他のものも出力したいのですが、別のAxios reqから出力したいのです。
<DropdownButton
title="Lägg till ny användare"
id="bg-nested-dropdown"
>
<MenuItem eventKey="1" style={{ marginLeft: '500px' }}>
{/*Different Axios data here!*/}
</MenuItem>
</DropdownButton>
そして、私は出力したいデータは、このコンポーネントで発見された:users.js
this.serverRequest = axios
.get('http://blablabla/customers') <-- Just a different endpoint
.then(res => {
// Rerender state
this.setState({
res,
dataToDisplay: res.data.slice(0, numToDisplay || 5),
})
})
などをdoorsItem.jsに私はuserItemにレイアウトの同じタイプを持っています.js:
const UsersItem = ({ item }) =>
<Row
key={item._id}
className="show-grid"
style={{ margin: '-15px 0 -15px 0' }}
>
<Col md={12}>
<ul style={{ marginTop: '10px' }}>
<li className="info-container">
<div>
<h5 className="title-text-container">{`${item.name} ${item.surname}`}</h5>
ここに私の質問があります。 doorsItem
に'http://blablabla/customers'
のデータを出力したい場合、最も簡単で「最良の」方法は何ですか?私はちょっと遊んだので、ドロップダウンをクリックすると、という1つのという名前が表示されます。その名前はIDに関連付けられているためです。しかし、私はちょうど'http://blablabla/customers'
からすべての名前をドロップダウンしたいです。
申し訳ありませんが、私の質問が乱雑です!しかし、読んでくれてありがとう!
返信いただきありがとうございます。私は共有されたReduxオブジェクトheheでどうやって行くのかについては極端に分かりません。 –
リアクション状態に限定されている場合、リアクションには一方向データフローがあることに注意してください。つまり、2つのコンポーネントが同じデータを共有したい場合、そのデータをそれらに渡す共通の祖先が必要です。参考にする必要があります:https://facebook.github.io/react/docs/thinking-in-react.html#step-4-identify-where-your-state-should-live – Tomasz