score:49
if you check the documentation for push
, there is an explanation of how different they are.
the push action adds a route on top of the stack and navigates forward to it. this differs from navigate in that navigate will pop back to earlier in the stack if a component is already mounted there. push will always add on top, so a component can be mounted multiple times.
we can take instagram for example;
consider navigating to a user's profile. then you can check user's followers and then you can navigate to their profile's too.
if you do this same actions with only navigate
action, when you click on an user's profile from the followers list screen will navigate to the previous profile but if you use push
it will push a new screen to the stack and show the correct profile. this way you can goback
to the first screen.
score:0
wanted to make it short and simple.
navigation.navigate
checks if the screen is in the stack so it takes there(to the older state) but navigation.push
just takes you to the new screen without checking if the screen was visited before and if that is available in the stack.
score:4
i think i can answer the question.
we have three pages:home page1 page2.
"home" is the "initialroutename",and "page1" and "page2" is child pages.
so when we start with home,and push page1 (or navigate page1),the page stack is:
(2). page1
(1). home
and i push three times page2,the stack is:
(5). page2
(4). page2
(3). page2
(2). page1
(1). home
now i want to go home, i can
1 pop four times, or pop(4) directly;
2 navigate page1, then pop one time;
3 poptotop one time;
when page stack has no page1,navigate works the same as push,push page to top of stack and show the page.
when page stack has page1,the function of navigate is to jump to the page1 which is closest to the top of the stack,and pop others pages above page1.
for example, the following page stack:
(7). page2
(6). page2
(5). page2
(4). page1
(3). page1
(2). page1
(1). home
if you wan to go back to home,you should navigate page1 firstly ,then pop three times.
tt should be noted that when current page is page1 and you navigate to page1,nothing
happened.
here are some of the tests code i wrote,you just copy to app.js.
import * as react from 'react';
import { button, view,text } from 'react-native';
import { navigationcontainer } from '@react-navigation/native';
import { createstacknavigator } from '@react-navigation/stack';
function homescreen({ navigation }) {
return (
<view style={{}}>
<button
title="navigate to page1"
onpress={() => navigation.navigate('page1',{"pagename":"page1"})}
/>
<button
title="push to page1"
onpress={() => navigation.push('page1',{"pagename":"page1"})}
/>
</view>
);
}
function page({route, navigation }) {
return (
<view style={{}}>
<text>{`current page is ${route.name}`}</text>
<button
title="navigate to page1"
onpress={() => navigation.navigate('page1')}
/>
<button
title="push page1"
onpress={() => navigation.push('page1')}
/>
<button
title="navigation to page2"
onpress={() => navigation.push('page2')}
/>
<button
title="push page2"
onpress={() => navigation.push('page2')}
/>
<button
title="go to homescreen"
onpress={() => navigation.navigate('home')}
/>
<button
title="pop"
onpress={() => navigation.pop()}
/>
<button
title="poptotop"
onpress={() => navigation.poptotop()}
/>
</view>
);
}
const stack = createstacknavigator();
function mystack() {
return (
<stack.navigator>
<stack.screen name="home" component={homescreen} />
<stack.screen name="page1" component={page} />
<stack.screen name="page2" component={page} />
</stack.navigator>
);
}
export default function app() {
return (
<navigationcontainer>
<mystack />
</navigationcontainer>
);
}
i hope it will be helpful to you.
score:14
according to the last blog post here: for v1:
navigate(routename) and push(routename) were very similar: every time you called navigate(routename) it would push a new route to the stack.
for v2:
now navigate(routename) will first try to find an existing instance of the route and jump to that if it exists, otherwise it will push the route to the stack.
navigate > go to instance of page if exist or push a new instance
push > push a new instance even if one exist already
Source: stackoverflow.com
Related Query
- React Navigation V2: Difference between navigation.push and navigation.navigate
- What is the difference between React Native and React?
- What's the difference between "super()" and "super(props)" in React when using es6 classes?
- React : difference between <Route exact path="/" /> and <Route path="/" />
- Difference between component and container in react redux
- Difference between React Component and React Element
- What's the difference between hydrate() and render() in React 16?
- What is the difference between NextJs and Create React App
- react router difference between component and render
- Difference between import React and import { Component } syntax
- What is the difference between hashHistory and browserHistory in react router?
- React Native - What is the difference between StyleSheet.absoluteFill() and StyleSheet.absoluteFillObject()?
- difference between React router.push and router.replace?
- What is the main difference between React Query and Redux?
- What's the difference between a JavaScript function and a React hook?
- React - What is the difference between renderToString and renderToStaticMarkup
- Difference between with and without useEffect in react functional component
- React navigation didfocus event listener works differently between class component and functional component
- What's the difference between importing React's Fragment from React and React, { Fragment }?
- React Hooks: What is the difference between 'useMutationEffect' and 'useLayoutEffect'?
- What is the difference between React component instance property and state property?
- What is the difference between React component and React component instance?
- React Native - Difference between onChangeText and onSubmitEditing?
- How does React router works and what is the difference between <link> and<Route>
- What is the difference between arrow functions and regular functions inside React functional components (no longer using class components)?
- Difference between React Context and JS singleton
- ES6 React - What are the difference between reference, shallow copy and deep copy and how to compare them?
- What is the difference between useHistory() and props.history in React Route
- what is the difference between getDefaultProps and getInitialState react js
- react - Difference between createMuiTheme and createTheme
More Query from same tag
- How to implement a CSS overlay which is partially transparent?
- Multiple fetch statements in componentWillMount
- How to use the response of useMutation in react-query to display data?
- React - Make Child component shrink when parent component shrinks
- Setting up forgotten password reset link (MERN stack)
- Expo TaskManager API: Updating state outside "export default"
- how to write type in typescript correctly
- Running a for loop inside a function called in react's render method
- SPFx ListView control - "defaultSelection" does not work
- React Router passing data from one component to another
- How to implement server side pagination with limit enforcement in Meteor
- How to pass the dynamic created props to the component?
- How to pass a react component to another react component, using ReactComponent?
- Component this.props logging undefined in console, but shows up in React Developer Tools
- Correct way to type a 'setState' function using React hook useState()
- Problem with useInterval and react hooks - infinite loop
- how can i change class based radio button group to functional based radio group
- removing previous object from array with useState method in react
- why we cannot pass boolean value as props in React , it always demands string to be passed in my code
- Remove by index value from props array
- How to change button color depending on array element in ReactJS
- How to re-render this.state after the server goes offline
- OpenLayers3 no refresh WMS in zoom-in
- Reactjs: TypeError: Cannot read property 'eventSlots' of undefined
- How can draw tick related style like major tick, tick-length, position, color in react-google-charts
- Trying to set & send a state variable as an URL string in a POST request with a single button click
- Button onclick does not working in reactJS with below code
- React - importing todos but I don't see it in the file
- using custom hook make re-render every time
- Animate presence not working in next js for exit animations only?