0

I would like to pass myVarible from Child component and retreive it in App (parent) component then put it on initialLang property.

Parent :

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;

Child :

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;

I dont know how to use props or state for do that when using hooks. Thanks for your help.

3
  • reactjs.org/docs/lifting-state-up.html Commented Jan 13, 2021 at 11:47
  • Good tuto, but I can't use constructor i havn't "class Chils extends React.Component" Commented Jan 14, 2021 at 9:58
  • How it has to do with that, understand the concept Commented Jan 14, 2021 at 10:06

1 Answer 1

1

In PARENT:

const [myVariable, setMyVariable] = useState('');
<ChildElement setMyVariable={setMyVariable}/>

In the Child

onClick={() => {props.setMyVariable('test123')}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.