3 回答

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊
這可以通過(guò)很多方式完成,考慮到上面共享的代碼具有基于類的實(shí)現(xiàn),我建議使用以下解決方案。
首先將函數(shù)傳遞給 Component1,將其命名為 dataPushed。
其次,在父級(jí)中創(chuàng)建一個(gè)狀態(tài)變量,將其命名為 dataPushed,將其初始化為 false。
第三,將函數(shù)和狀態(tài)變量從父級(jí)傳遞到Concept2,分別將其命名為dataFetched和petchData。
現(xiàn)在 Component1 將有自己的函數(shù)來(lái)推送數(shù)據(jù),讓我們將此函數(shù)稱為 pushData。推送數(shù)據(jù)的邏輯結(jié)束后,立即調(diào)用傳遞的 prop 函數(shù) dataPushed。此 props 將使用當(dāng)前狀態(tài)更新父狀態(tài),即推送數(shù)據(jù)并將狀態(tài)變量設(shè)置為 true。
現(xiàn)在,此狀態(tài)變量已使用組件 2 中的組件 DidUpdate 傳遞給組件 2。我們可以從組件 1 知道 dataPush 的狀態(tài),如果這是真的,你可以調(diào)用 Component2 內(nèi)部函數(shù)來(lái)獲取數(shù)據(jù)。
獲取數(shù)據(jù)后,立即調(diào)用傳遞給此 Component2 的 prop,讓父級(jí)知道已獲取最新數(shù)據(jù),并將 dataPushed 的父狀態(tài)變量設(shè)置為 false。
忽略使用的函數(shù)和變量名稱,隨意使用您的。
我已經(jīng)用代碼創(chuàng)建了一個(gè)沙盒,
https://codesandbox.io/s/affectionate-forest-5kc68?file=/src/App.js
我希望這能解決您的問(wèn)題。如果您仍然卡住或不清楚上述任何解釋,請(qǐng)告訴我。

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
從建筑上講,你試圖做的事情不是一個(gè)好主意。
例如,如果更改具有要使用的函數(shù)的組件,則需要記住更新使用此函數(shù)的任何依賴項(xiàng)。這變得難以維護(hù)。
React props 實(shí)際上使得使用依賴注入模式來(lái)解決這個(gè)問(wèn)題變得非常容易。如果你不熟悉依賴注入,這基本上意味著依賴關(guān)系來(lái)自自上而下,需要這些依賴關(guān)系的組件不知道這個(gè)依賴關(guān)系在哪里,也不會(huì)出去獲取它們。它只是將依賴項(xiàng)傳入。
您可以把函數(shù)提升一個(gè)級(jí)別,讓它位于父組件中,而不是嘗試使用位于同級(jí)組件中的方法。國(guó)家也一樣。
想象一下這個(gè)父母:
class Container extends React.Component {
state = {
counter: 0
};
reallyCoolFunction = () => {
this.setState({ counter: this.state.counter + 1 });
};
render() {
return (
<>
<Cool
counter={this.state.counter}
doCoolThing={this.reallyCoolFunction}
/>
<Wannabe doCoolThing={this.reallyCoolFunction} />
</>
);
}
}
上述組件創(chuàng)建函數(shù)并將其傳遞給兩個(gè)組件。然后,每個(gè)組件將在需要時(shí)調(diào)用該函數(shù),例如:Container
class Wannabe extends React.Component {
handleClick = () => {
this.props.doCoolThing();
};
render() {
return (
<div>
<h2>Wannabe</h2>
<button onClick={this.handleClick}>click</button>
</div>
);
}
}
然后,應(yīng)用中擁有該函數(shù)的組件將傳遞到相關(guān)的道具:
class Cool extends React.Component {
handleClick = () => {
this.props.doCoolThing();
};
render() {
return (
<div>
<h2>Cool</h2>
<p>{this.props.counter}</p>
<button onClick={this.handleClick}>click</button>
</div>
);
}
}
如果你的應(yīng)用正在增長(zhǎng),并且你計(jì)劃在一段時(shí)間內(nèi)使用它,則可能需要考慮實(shí)現(xiàn)像 Redux 這樣的狀態(tài)管理解決方案。它使?fàn)顟B(tài)管理更容易。
如果您要使用功能組件而不是類組件,則可以使用第三個(gè)選項(xiàng),那就是構(gòu)建自己的自定義鉤子。如果你還不熟悉鉤子,那可能是一個(gè)學(xué)習(xí)曲線。你可以閱讀 React hooks 文檔,了解如何做到這一點(diǎn)。
我創(chuàng)建了容器組件和子組件示例的演示。在此示例中,它們都能夠更新組件中顯示的狀態(tài):https://codesandbox.io/s/dry-hill-r425r?file=/src/App.jsCool

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
在組件 1 中訪問(wèn)組件 2 函數(shù)的一種方法是使用 。refs
工作演示在這里。
代碼段:
export default class App extends React.Component {
ref = React.createRef();
render() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<Component1 comp2Ref={this.ref} />
<Component2 ref={this.ref} />
</div>
);
}
}
// Component1.js
export default class Component1 extends React.Component {
pushData = () => {
// make the first api call..
console.log("comp 1 fun");
// now call component2 function
console.log(this.props);
this.props.comp2Ref.current.fetchData();
};
render() {
return (
<div className="App">
<h1>component 1</h1>
<button onClick={this.pushData}>push data button</button>.
</div>
);
}
}
// Component2.js
export default class Component2 extends React.Component {
fetchData = () => {
console.log("comp 2 fun");
};
render() {
return (
<div className="App">
<h1>component 2</h1>
</div>
);
}
}
處理您的方案的最佳方法是將函數(shù)提升到父級(jí),并將其作為 prop 傳遞給 .fetchDatacomponent2
喜歡這個(gè)
export default class App extends React.Component {
state = {
data: []
};
fetchData = () => {
// fetch the data and update the state(data) and pass it as prosp in component 2
console.log("comp 2 fun");
};
render() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<Component1 fetchData={this.fetchData} />
<Component2 data={this.state.data} fetchData={this.fetchData} />
</div>
);
}
}
// Component 1 pushData function
...
pushData = () => {
// make the first api call..
console.log("comp 1 fun");
// now call component2 function
console.log(this.props);
this.props.fetchData();
};
...
添加回答
舉報(bào)