使用 Hooks 将数据从子组件传递到父组件

时间:2021-01-13 11:44:05

标签: reactjs react-hooks state native

我想从 Child 组件传递 myVarible 并在 App(父)组件中检索它,然后将它放在 initialLang 属性上。

家长:

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;

我不知道在使用钩子时如何使用道具或状态来做到这一点。感谢您的帮助。

0 个答案:

没有答案
相关问题