Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React router, codes after history.push run?

Say on your clickHandler

you do

// url change will cause this component to be dismounted and a new component will mount
history.push(url)

// do some more work 
dispatch(someMoreWork())

What happens to the code after the push call? is the code guaranteed to run?

like image 853
eugene Avatar asked May 17 '20 04:05

eugene


People also ask

How to navigate programmatically in react with history?

This library will allow you to navigate programmatically in React by attaching the history object as a property which allows you to call it from within your React component. The history.push function takes in the path you want to navigate to, and then and state that you want to pass along.

How to add react router in react app?

React Router is the most popular solution. To add React Router in your application, run this in the terminal from the root directory of the application: Note: This tutorial uses React Router v6.

How to enable routing in ReactJS?

As you may already know, by default, React comes without routing. And to enable it in our project, we need to add a library named react-router. To install it, you will have to run the following command in your terminal:

What is react-router?

That means the user can move between different parts of an application by entering a URL or clicking on an element. As you may already know, by default, React comes without routing. And to enable it in our project, we need to add a library named react-router.


1 Answers

history navigation is done only after the sync action within the event queue in javascript are executed.

In your case, javascript will executed all the things within the function where you call history.push and will then navigate to the new Route and it will be a predictable behaviour

You can simply validate the above by executing the dispatch action and then accessing the value in the rendered component.

Check this demo that demonstrates the above behavior by dispatching an action after history.push and accessing the resultant redux state in the rendered route

like image 140
Shubham Khatri Avatar answered Sep 22 '22 00:09

Shubham Khatri