Skip to content
the poor coder

react navigation params

Poor Man
Published
Reading time
1 min read
On this page

React Navigation Params#

If you are building a React Native application and you want to pass data between screens, React Navigation Params is the way to go. Params allow you to pass data between screens, set up initial values for your screen components and control the behavior of your navigation flow.

Passing Params Between Screens#

To pass params between screens, you can use the navigation.navigate function with an object containing the params you want to pass:

Text
navigation.navigate('Profile', {
  userId: '123',
});

And then, in the receiving screen, you can access the params through the route.params object:

Text
function ProfileScreen({ route }) {
  const { userId } = route.params;
  return (
    <View>
      <Text>User ID: {userId}</Text>
    </View>
  );
}

Setting Up Initial Params#

You can also set up initial params for your screens using the initialParams prop in your screen component:

Text
function ProfileScreen({ route }) {
  const { userId } = route.params;
  return (
    <View>
      <Text>User ID: {userId}</Text>
    </View>
  );
}

ProfileScreen.defaultProps = {
  route: {
    params: {
      userId: '456',
    },
  },
};

Controlling Navigation Behavior#

You can also control the behavior of your navigation flow with params. For example, you can set up a param that determines whether a screen should be displayed or not:

Text
navigation.navigate('Profile', {
  showProfile: true,
});

And then, in the receiving screen, you can use the useEffect hook to conditionally display the screen:

Text
function ProfileScreen({ route }) {
  const { showProfile } = route.params;

  useEffect(() => {
    if (!showProfile) {
      navigation.goBack();
    }
  }, [showProfile]);

  return (
    <View>
      <Text>This is the profile screen!</Text>
    </View>
  );
}

These are just a few examples of what you can do with React Navigation Params. With params, you can build complex navigation flows and pass data seamlessly between screens.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC