3 回答

TA貢獻1847條經驗 獲得超11個贊
我更喜歡兩種將變量傳遞給子組件的方法。它們在不同的情況下都有用
方法一:使用屬性 => Props
如果您的組件樹嵌套不深,則此方法很有用。例如,您希望將變量從父項傳遞給子項。
一個嵌套的組件如下
const ParentComponent = () => {
const [variable, setVariable] = useState(0);
return (
<ChildComponent variable={variable} setVariable={setVariable} /> //nested within ParentComponent, first level
)
}
const ChildComponent = (props) => {
return(
<>
<div>prop value is {props.variable}</div> //variable attribute available through component props
<button onClick={props.setVariable(prevValue => prevValue+1}>add +1</button> //set value in parent through callBack method
</>
)
}
如果你有一個高度嵌套的組件層次結構,事情就會變得有點混亂。比方說, ChildComponent 返回另一個組件,并且您希望variable將 傳遞給該組件,但是 ChildComponent 不需要該變量,您最終會遇到這種情況
const ParentComponent = () => {
const [variable, setVariable] = useState(false);
return (
<ChildComponent someProp={variable}/> //nested within ParentComponent, first level
)
}
const ChildComponent = (props) => {
return(
<AnotherCustomComponent someProp={props.someProps}/> //someProp attribute available through component props
)
}
const AnotherCustomComponent = (props) => {
return(
<div>prop value is {props.someProp}</div> //someProp attribute available through component props
)
}
即使 ChildComponent 不需要該道具,它也需要通過道具將其推送到其子組件。這被稱為“螺旋槳鉆井”。這是一個簡單的示例,但對于更復雜的系統,它可能會變得非?;靵y。為此,我們使用...
方法二:Context API CodeSandbox
上下文 API 提供了一種向子組件提供狀態的巧妙方法,而不會以道具鉆井情況結束。它需要一個Provideris setup,它將它的值提供給它的任何Consumers'. Any component that is a child of the Provider 可以使用上下文。
首先創建一段上下文。
CustomContext.js
import React from 'react';
const CustomContext = React.createContext();
export function useCustomContext() {
return React.useContext(CustomContext);
}
export default CustomContext;
接下來是實現提供者,并給它一個值。我們可以使用之前的 ParentComponent 并添加 Context provider
import CustomContext from './CustomContext'
const ParentComponent = () => {
const [variable, setVariable] = useState(false);
const providerState = {
variable,
setVariable
}
return (
<CustomContext.Provider value={providerState} >
<ChildComponent />
</CustomContext.Provider>
)
}
現在任何嵌套在 <CustomContext.Provider></CustomContext.Provider> 中的組件都可以訪問傳遞到Provider
我們嵌套的子組件看起來像這樣
const ChildComponent = (props) => {
return(
<AnotherCustomComponent/> //Notice we arent passing the prop here anymore
)
}
const AnotherCustomComponent = (props) => {
const {variable, setVariable} = useCustomContext(); //This will get the value of the "value" prop we gave to the provider at the parent level
return(
<div>prop value is {variable}</div> //variable pulled from Context
)
}
如果 ParentComponent 被使用兩次,則 ParentComponent 的每個實例都將有自己的“CustomContext”可供其子組件使用。
const App() {
return (
<>
<ParentComponent/>
<ParentComponent/>
</>
}

TA貢獻1794條經驗 獲得超8個贊
您可以使用回調函數來執行此操作。
您基本上將一個函數傳遞給您的子組件,您的子組件將觸發該函數,而父組件將具有該值。
這是一個應該如何實現的簡單示例:
家長:
const Parent = () => {
const onSearchResult = searchResults => {
console.log(searchResults)
}
return (
<>
I am the parent component
<Child onSearchResult={onSearchResult} />
</>
)
}
孩子:
const Child = onSearchResult => {
const calculateResults = e => {
const results = doSomeStuff(e)
onSearchResult(results)
}
return (
<>
I am the child component
I have a component that will return some value
<input onKeyPress={e => calculateResults(e)}
</>
)
}

TA貢獻1777條經驗 獲得超10個贊
子組件應該從父組件那里獲取一個回調屬性。有點像按鈕的工作方式:
<Button onClick={this.onButtonClick}
你想要的是做
<SearchComponent onSearchResults={this.onResults}
然后,在搜索組件中,您可以調用this.props.onSearchResults(searchResults);
添加回答
舉報