I'm building my first React Native app with react navigation (coming from a web react background).
Normally, I just useState for much of what I handle. For example, rendering a list of components based on the number of objects returned by a database query (Friend List type feature) or for a multi-step user sign up (each page adds some information to the state, and the last one sends all the information in a post request).
However, it seems I can't do that with react native ? I'm using React navigation, so every screen component I make is wrapped inside a <Stack.Screen> component. How can I pass props so that I can update the state of parent components ? Do i necessarily need redux for this ?
Here's a description of how i previously did this in React. The feature is a 5 stage sign up process.
The user navigates to the signup page, components for url/page1 load. They input their name and email and hit next. The button updates the app.state using the setState prop it received from it's parent. It then navigates to url/page2.
url/page2 components have the user input their address. The user hits next, the button updates the state and navigates to the next one. Etc.
Each new page adds new information to the state until the final "submit" button takes all this information and submits it via a POST request to the backend.
In react native, my app.js is organized like this:
<NavigationContainer>
<Stack.Navigator initialRouteName = "TempHome">
<Stack.Screen name="TempHome" component={TempHome} />
<Stack.Screen name="Welcome" component={Welcome} />
<Stack.Screen name="LogIn" component={LogIn} />
<Stack.Screen name="SignUp" component={SignUp} />
<Stack.Screen name="ProfileSetupA" component={ProfileSetupA} />
<Stack.Screen name="ProfileSetupB" component={ProfileSetupB} />
<Stack.Screen name="ProfileSetupC" component={ProfileSetupC} />
<Stack.Screen name="ProfileSetupD" component={ProfileSetupD} />
<Stack.Screen name="ProfileSetupE" component={ProfileSetupE} />
<Stack.Screen name="ProfileSetupF" component={ProfileSetupF} />
<Stack.Screen name="ProfileSetupG" component={ProfileSetupG}/>
<Stack.Screen name="ProfileSetupH" component={ProfileSetupH}/>
<Stack.Screen name="PetProfileHome" component={PetProfileHome} />
<Stack.Screen name="PetProfileScheduledActivities" component={PetProfileScheduledActivities} />
<Stack.Screen name="PetProfileAccount" component={PetProfileAccount} />
<Stack.Screen name="PetProfileTraits" component={PetProfileTraits} />
<Stack.Screen name="PetProfileInvitation" component={PetProfileInvitation} />
<Stack.Screen name="Notification" component={Notification} />
<Stack.Screen name="NetworkExample" component={Network} />
<Stack.Screen name="MapExample" component={MapExample} />
<Stack.Screen name="ShareExample" component={ShareExample} />
</Stack.Navigator>
</NavigationContainer>
Each of these screens navigates to the next one in the list using const ProfileSetupF = () => navigation.navigate('ProfileSetupF')
How can use my state props as previously in this context ? I don't want all my setup screens to be in the same component or stack.Screen because I want the user to be able to use the back button and the nice animations that come with pushing a new screen to the stack. I've been told I should use Redux for this. Any advice ?
navigation.navigate('ProfileSetupF', { myPrevState: 'anything' })myPrevStatein your next screen.const { myPrevState } = route.params. You can access route with props, or use the hooksuseRoute. passing params reactnavigation.org/docs/params , route hooks reactnavigation.org/docs/use-route