Questions tagged [react-router]

React Router - A complete routing library for React inspired by Ember's routing system

Introduction

React Router is a powerful routing library built on top of React that helps you add new screens and flows to your application incredibly quickly, all while keeping the URL in sync with what's being displayed on the page.

Example

import React from 'react'
import ReactDOM from 'react-dom'
import {
    BrowserRouter as Router,
    Route,
    Link
} from 'react-router-dom'

const BasicExample = () => (
    <Router>
        <div>
            <ul>
                <li><Link to="/">Home</Link></li>
                <li><Link to="/about">About</Link></li>
                <li><Link to="/topics">Topics</Link></li>
            </ul>
            <hr/>

            <Route exact path="/" component={Home}/>
            <Route path="/about" component={About}/>
            <Route path="/topics" component={Topics}/>
        </div>
    </Router>
);

const Home = () => (
    <div>
        <h2>Home</h2>
    </div>
);

const About = () => (
    <div>
        <h2>About</h2>
    </div>
);

const Topics = ({ match }) => (
    <div>
        <h2>Topics</h2>
        <ul>
            <li>
                <Link to={`${match.url}/rendering`}>
                    Rendering with React
                </Link>
            </li>
            <li>
                <Link to={`${match.url}/components`}>
                    Components
                </Link>
            </li>
            <li>
                <Link to={`${match.url}/props-v-state`}>
                    Props v. State
                </Link>
            </li>
        </ul>

        <Route path={`${match.url}/:topicId`} component={Topic}/>
        <Route exact path={match.url} render={() => (
            <h3>Please select a topic.</h3>
        )}/>
    </div>
);

const Topic = ({ match }) => (
    <div>
        <h3>{match.params.topicId}</h3>
    </div>
);

ReactDOM.render(<BasicExample />, document.body)

Docs


Related tags

20178 questions
6
votes
1 answer

Passing props from react router to children on the server

I'm building an Isomorphic app using React, react-router v3 and material-ui. One of the requirements of material-ui in server-side rendering is to pass to the theme the client's user agent, so MUI will be able to prefix their inline styles…
Gershon Papi
  • 5,008
  • 3
  • 24
  • 50
6
votes
2 answers

how to send value one component to another component in react js?

could you please tell me how to send input field value on second component on button click .I have one button and input field in first component.On button click I need to send input field value to second component here is my…
user944513
  • 12,247
  • 49
  • 168
  • 318
6
votes
2 answers

Keep components between routes using react-router

I have a multi-steps form and I'm using react-router to navigate between the different steps. In some of the steps I show an iframe to the user. When the user navigates between steps it always unmount and re-mount the iframe, this causes two…
E. Maimon
  • 61
  • 1
  • 3
6
votes
1 answer

Relay/router login mutation?

I'm trying to implement a mutation for login. The mutation validates the provided id_token and logs the user in via sessions. The mutation itself works (verified with GraphiQL), but I'm having issues integrating it with Relay. When a user logs in,…
Sam P
  • 1,821
  • 13
  • 26
6
votes
2 answers

React router path validation and redirection

I'm using react-router with react-router-redux for implementation of SPA. I want to implement such a UI: The main idea that I have a list of Entitnes and if I click on an Entity, url is changed from http://domain/entities to…
Pavlo Hryza
  • 627
  • 1
  • 11
  • 18
6
votes
1 answer

Setting up code splitting in Webpack and React.js

I'm trying to set up code splitting / chunking in my app - by route, using require.ensure. So here's my route: {require.ensure( [], (require) => {…
Dmitry Shvedov
  • 3,169
  • 4
  • 39
  • 51
6
votes
1 answer

Can't make react-hot-loader and webpack-dev-server work with react-router

I'm trying to use react-hot-loader with webpack-dev-server and react-router, but when I try to access localhost:3000/ I get : Cannot GET / Of course, it works when I try to access localhost:8000/. I tried to follow react-hot-boilerplate, without…
6
votes
3 answers

How to make page slide(change) with react-router

I'v tried to use react-router and ReactTransitionGroup to make an navigation effect(page slide whereas route changes). However, it's error-prone and ugly.(made much logic to define which direction to slide to and remove/add classes to make…
D.jennis
  • 169
  • 1
  • 10
6
votes
4 answers

React setState() not updating state after $.ajax() request

I'm using react with react-router. After checking authentication with onEnter Asynchronous hook on IndexRoute, App component gets rendered. App component has an initial state auth which is set to undefined when it renders. auth state is being passed…
Waleed Ahmad
  • 2,184
  • 4
  • 21
  • 23
6
votes
2 answers

Can we cancel out api response when the react router is changed?

I am using same form and same state in redux for add and edit. When I call api for fetching data for edit and we change our router to add form before the response arrives. All the form datas will be auto filled for add item since i am using same…
6
votes
4 answers

How to show loading UI when calling getComponent in react-router?

I'm really new to React and I can't figure out how to render a "loading..." screen when a route is being loaded with getComponent. The getComponent call works fine and displays the component, but there's no indication on the UI that anything is…
David M
  • 71
  • 1
  • 4
6
votes
2 answers

React router parametrized routes: SyntaxError: expected expression, got '<'

I have a React-router set up which uses parametrized routes: The error: SyntaxError: expected…
user3104270
  • 625
  • 2
  • 10
  • 17
6
votes
1 answer

this.props.history is deprecated (react-router)

I was trying to programatically navigate to a different page like so this.props.history.push('/new-path'); it worked, but I got a deprecation warning in console saying: Warning: [react-router] props.history and context.history are deprecated.…
Ilja
  • 44,142
  • 92
  • 275
  • 498
6
votes
2 answers

How to change parent-component style element according to its child-component state?

I'm trying to figure out how to set style to parent component while child component state change consider a scenario in which we have a container component containing menu and side bar as its static elements plus child-component. while…
Fakhruddin Abdi
  • 876
  • 2
  • 11
  • 25
6
votes
1 answer

How can I use react-router to trigger state changes?

I'm still coming to grips with React and react-router, but I'm wondering how to use react-router to simply update application state without re-rendering DOM nodes. For example, suppose I have:
brianskold
  • 809
  • 5
  • 10