0

I know there's many similar threads but since I have a hard time understanding the answers I figured I might try with my own code and see if I can make any sense of the answers.

I just set this up really simple to test it out. I have an Index file that is opened when I start the app. In the index I have testValue in this.state:

Update:

In SignIn:

 constructor(props){
        super(props);
        this.state={
          credentials: {
            username: "",
            password:"",

          }
        }
        this.navigate = this.props.navigation.navigate;

    {...}

    this.navigate("main", {
                credentials: this.state.username,

              });

In main:
    constructor(props) {
            super(props);
        this.params = this.props.navigation.state.params;
        this.navigate = this.props.navigation.navigate;

       {...}

    render() {
        console.log(this.params.username);
12
  • you didn't show the code where you are passing the prop. show the render method where TestIndex is used Commented Apr 28, 2018 at 21:26
  • I am not passing the prop. How do I do that? I just have it in this.state Commented Apr 28, 2018 at 21:30
  • 1
    If you want 'testValue' to be a prop, then when you use TestIndex from Index, you have to do something like <TestIndex testValue={this.state.testValue}> Commented Apr 28, 2018 at 21:31
  • required reading reactjs.org/docs/… Commented Apr 28, 2018 at 21:33
  • <TestIndex testValue={this.state.testValue}> Do I pass this on somehow with the button or just paste that somewhere? Yes, I understand I sound like a moron but I just can't wrap my head around this. Commented Apr 28, 2018 at 21:39

1 Answer 1

1

This would actually log the testValue. All you have to do is to pass the testValue state as a prop in TestIndex component. Like this:

Index.jsx

export default class Index {

    constructor(props) {
        super(props);
        this.state = {
            testValue: 'hello'
        }
    }

    render() {
        return <TestIndex testValue={this.state.testValue} />
    }
}

TestIndex.jsx

export default class TestIndex extends React.Component {
    index() {
        this.props.navigation.navigate("index")
    }
    handleClickMain = () => {
        this.props.navigation.navigate("index");
    }
    render() {
        return (
            <View style={styles.container}>
                <TouchableOpacity style={styles.btn} onPress={() => {
                    console.log(this.props.testValue) //here's where I want it to log testValue from the index file
                    this.handleClickIndex()
                }
                }>
                </TouchableOpacity>
            </View>
        );
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I feel like I'm very close with your response, thanks for that. I have (a lot) more than that in the index render though and when I do that I just go directly to the TextIndex file instead of clicking my way there. Is it possible to return <TestIndex testValue={this.state.testValue} /> within the button {} or should I createClass somehow? Again, thanks for your comment, I really thought I got it this time

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.