Jules
Jules

Reputation: 115

React Router protected routes

I'm learning React Router (protected routes, to be precise) and in React Router documentation there is a function which I almost understand but there is one line of code which I can't see how it works. Maybe someone can shortly describe what that line does. Below is a function from https://reactrouter.com/web/example/auth-workflow

function LoginPage() {
  let history = useHistory();
  let location = useLocation();

  let { from } = location.state || { from: { pathname: "/" } };
  let login = () => {
    fakeAuth.authenticate(() => {
        history.replace(from);
    });
  }; 

What does this line do?

let { from } = location.state || { from: { pathname: "/" } };

I understand that we are creating an object but what does || do? Is it connecting two objects into one? I don't get it.

Upvotes: 0

Views: 116

Answers (1)

ludwiguer
ludwiguer

Reputation: 2245

|| is the logical OR operator

let { from } = location.state || { from: { pathname: "/" } };

This is saying, assign location.state.from to the variable from if location.state is not null or undefined, else assign { pathname: "/" }

Upvotes: 2

Related Questions