React Native - Passing Data Across Screens
Asked Answered
K

4

8

I'm having some trouble with a react-native app. I can't figure out how to pass data across screens.

I realize that there are other similar questions that are answered on SO, however the solutions did not work for me.

I'm using the StackNavigator. Here's my setup in my App.js file.

export default SimpleApp = StackNavigator({
    Home: { screen: HomeScreen },
    Categories: { screen: CategoriesScreen }, // send from here
    Category: { screen: CategoryScreen }      // to here
});

I have a TouchableHighlight component which has a onPress event that will navigate to the desired screen. This is on my Categories.js file/screen.

<TouchableHighlight onPress={(id) => {
    const { navigate } = this.props.navigation;
    navigate('Category', { category: id });
}}>
    <Text>{name}</Text>
</TouchableHighlight>

When I click the element, the screen does indeed switch to category, however it fails to send the props data.

So when I check the data in my Category screen, it returns undefined. I have tried the following methods:

this.props.category
this.props.navigation.state.category;
this.props.navigation.state.params.category

How exactly can I access that data that I passed in the navigate method?

navigate('Category', { category: id });

Edit: Here is my actual code structure:

The data comes from a API.

for (var i = 0; i < data.length; i++) {
    var name = data[i].name;
    var id = data[i].id;
    categoryComponents.push(
        <Card key={id}>
            <CardItem>
                <Body>
                    <TouchableHighlight onPress={(id) => {
                        const { navigate } = this.props.navigation;
                        navigate('Category', { params: { category: id } });
                    }}>
                        <Text>{name + " " + id}</Text>
                    </TouchableHighlight>
                </Body>
            </CardItem>
        </Card>
    );
}
Kooima answered 2/10, 2017 at 13:56 Comment(0)
T
5

Your problem isn't sending the parameter. You are sending it right and reading it right. Your error is related to that your id is undefined.

You should fix your code like below,

for (var i = 0; i < data.length; i++) {
    var name = data[i].name;
    var id = data[i].id;
    categoryComponents.push(
        <Card key={id}>
            <CardItem>
                <Body>
                    <TouchableHighlight onPress={(event) => {
                        // onPress event fires with an event object
                        const { navigate } = this.props.navigation;
                        navigate('Category', { category: id });
                    }}>
                        <Text>{name + " " + id}</Text>
                    </TouchableHighlight>
                </Body>
            </CardItem>
        </Card>
    );
}

And you can read your parameter like below.

this.props.navigation.state.params.category
Tula answered 2/10, 2017 at 14:11 Comment(2)
Much thanks! I've been stuck on this fire awhile. Was I overriding the id variable because I made it a parameter?Kooima
Yes you were overriding parameters so the id you were trying to pass was not actually the id you created in your for loop.Tula
R
0

From parent screen:

navigate({ routeName: 'Category',  params: { category: id } })

or

navigate('Category', { category: id })

From child screen:

const category = this.props.navigation.state.params.category

Rupture answered 2/10, 2017 at 13:58 Comment(6)
I just tried this, however it still seems to be returning undefined.Kooima
Where does your id come from in <TouchableHighlight onPress={(id) => {...?Rupture
This is wrong I'm afraid. Parameter passing is wrong.Tula
You were missing a parenthesis. I edited the answer.Tula
The updated answer seems to be worse. The page does not navigate or send the data. Nothing happens.Kooima
Are you using Redux?Rupture
C
0

So looking at the above described issue I think you are looking for something like screenProps (documented here).

This allows you to pass a specific props across screens.

I hope this helps. Please let me know if you need anything else.

Chema answered 2/10, 2017 at 14:12 Comment(0)
S
0

Using the following code snippet, you can navigate to another screen with some values.

navigate({ Home: 'HomeScreen',  params: { username: 'abcd' } })

and you can collect the parameter at HomeScreen using,

this.props.navigation.state.params.username
Static answered 26/3, 2019 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.