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
2 answers

how to create store with react-redux

Sorry for the silly question but i do not know how to combine together my existing redux store definition with applyMiddleware. This is my current working code: const store = createStore( combineReducers({ ...reducers, routing:…
zappee
  • 20,148
  • 14
  • 73
  • 129
6
votes
1 answer

React router Link wrong url

I am using React router with Link to change urls and navigate trough app. In list of readings I navigate user to reading edit with this code: I have defined the following…
alphiii
  • 1,597
  • 3
  • 21
  • 27
6
votes
2 answers

Start the ReactJS App without port

I am working on ReactJS application using create-react-app. The npm start and npm run build application is working fine, The application is running in a port number like 4000 or pushstate-server build in the port 9000, The problem is that I need to…
Venkatesh Somu
  • 4,710
  • 4
  • 23
  • 22
6
votes
1 answer

Redux state doesn't update with action dispatch

I've gone through every piece of documentation and example project I can find for building a react app with redux and react-router, but I just can't seem to figure out how to get my redux state to update when I dispatch actions. As you can see in…
DGaffneyDC
  • 152
  • 1
  • 2
  • 12
6
votes
0 answers

How to isomorphically handle subdomain with react + router + redux?

I'm building a service where other companies can host a community at companyname.mydomain.com. I have a Redux reducer called company that holds certain state about that company. I'd like to be able to isomorphically fetch data about that company…
6
votes
2 answers

react-router : share state between Routes without Redux

I would like to have a shared state (list of clients fetched remotely) between 2 sibling Routes : Timesheets and Clients. I want to try how far i can go with 'pure' React (No Flux architecture). This example works, but I have an error :…
gr3g
  • 2,866
  • 5
  • 28
  • 52
6
votes
1 answer

How to handle exceptions thrown in react-router child routes

I'm having an issue with exception handling with react-router@3.0.0 My router is attached like this: try { ReactDOM.render(
Charlie
  • 81
  • 5
6
votes
1 answer

Graphql, react-apollo how to transfer variable to query at loading component state

I have a simple react component that must load data from server when user ask it. Problem is that i don't know how to transfer dynamic variable speakerUrl and access it in before component load state. Sure i can access it from this.props.params, but…
SLI
  • 713
  • 11
  • 29
6
votes
3 answers

What does {...props} in this React Component mean?

Just starting out with react-router. I'm using react-router@next (version 4) when I came across this bit of code in github (at the bottom). I have weak React + ES6-fu thus require your help. Does {...props} here refer to sending props to the…
Snail
  • 97
  • 1
  • 6
6
votes
1 answer

How to get current location from react-router?

I am trying to drive my application's navigation from its state. Here's my use case: Given the user is on the Login page When the application state changes to isLoggedIn Then the application should navigate to the Dashboard page Here's my current…
Naresh
  • 23,937
  • 33
  • 132
  • 204
6
votes
1 answer

History object issue with React-Router

I am building a very simple webpage using React and React Router. I have installed the latest version of React Router module (v3.0.0) using NPM, written 3 very simple routes in my index.js file: import React, {Component} from 'react'; import…
Maxine Ellah
  • 175
  • 3
  • 11
6
votes
5 answers

React, Redux, React-Router?

The question of the application architecture. Suppose there are a lot of components (look at the picture) (mains) on a page. What better use to switch the main children's components (active / not active)? And pages (1, 2, 3, next)? Can I use…
evgkch
  • 637
  • 1
  • 5
  • 7
6
votes
1 answer

React Router - Invalid prop `children` supplied to `Router` when composing routes

Why is it that when I supply the routes within the router all is good but when I compose the routes like below I get a warning.js:36 Warning: Failed prop type: Invalid propchildrensupplied toRouter. in Router (at index.js:62) in…
bluengreen
  • 61
  • 1
  • 2
6
votes
3 answers

Default component in nested routes in React Router

In React Router I have a nested Route So now it shows Team when I go to /about/team. But how do I set which Component to be seen when I…
Jamgreen
  • 10,329
  • 29
  • 113
  • 224
6
votes
2 answers

Enable single page app react hot reload webpack

I have some problems setting up a single page react app using react router with the webpackdevserver. If I use browserhistory webpack has some problems when entering a nested route url (/client/view for example). This could be solved adding…