Reactjs 使用挂钩将数据从子组件传递到父组件
我想从子组件传递MyVariable,并在应用程序(父)组件中检索它,然后将其放在initialLang属性上 家长:Reactjs 使用挂钩将数据从子组件传递到父组件,reactjs,react-hooks,state,native,Reactjs,React Hooks,State,Native,我想从子组件传递MyVariable,并在应用程序(父)组件中检索它,然后将其放在initialLang属性上 家长: imports ... // Here there is not "class App extends React.Component {" !!! const App = () => ( <Provider store={store}> <I18n translations={translations}
imports ...
// Here there is not "class App extends React.Component {" !!!
const App = () => (
<Provider store={store}>
<I18n translations={translations} initialLang="en" fallbackLang="en"></I18n>
</Provider>
);
export default App;
imports ...
// Here there is not "class Child extends React.Component {" !!!
const ChildInner = ({context}) => (
{
<View style={{flexDirection: "row"}}>
<Picker
onValueChange={(itemValue, itemIndex) => {
if ( itemIndex == 0 ) {
myVariable = 'en';
} else if ( itemIndex == 1 ) {
myVariable = 'fr';
}
}}
>
<Picker.Item label="English" value="en" />
<Picker.Item label="french" value="fr" />
</Picker>
</View>
}
);
const Child = (context) => (
...
);
Child.propTypes = {
....
};
Child.contextTypes = {
t: PropTypes.func.isRequired,
};
export default Child;
导入。。。
//这里没有“类应用程序扩展React.Component{”!!!
常量应用=()=>(
);
导出默认应用程序;
子项:
imports ...
// Here there is not "class App extends React.Component {" !!!
const App = () => (
<Provider store={store}>
<I18n translations={translations} initialLang="en" fallbackLang="en"></I18n>
</Provider>
);
export default App;
imports ...
// Here there is not "class Child extends React.Component {" !!!
const ChildInner = ({context}) => (
{
<View style={{flexDirection: "row"}}>
<Picker
onValueChange={(itemValue, itemIndex) => {
if ( itemIndex == 0 ) {
myVariable = 'en';
} else if ( itemIndex == 1 ) {
myVariable = 'fr';
}
}}
>
<Picker.Item label="English" value="en" />
<Picker.Item label="french" value="fr" />
</Picker>
</View>
}
);
const Child = (context) => (
...
);
Child.propTypes = {
....
};
Child.contextTypes = {
t: PropTypes.func.isRequired,
};
export default Child;
导入。。。
//这里没有“类子级扩展React.Component{”!!!
const ChildInner=({context})=>(
{
{
如果(itemIndex==0){
myVariable='en';
}else if(itemIndex==1){
myVariable='fr';
}
}}
>
}
);
const Child=(上下文)=>(
...
);
Child.propTypes={
....
};
Child.contextTypes={
t:PropTypes.func.isRequired,
};
导出默认子对象;
我不知道使用挂钩时如何使用道具或状态。感谢您的帮助。在家长:
const [myVariable, setMyVariable] = useState('');
<ChildElement setMyVariable={setMyVariable}/>
很好的图图,但我不能使用构造函数,我没有“类Chils extensed React.Component”它与此有什么关系,理解这个概念