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

React native: Always running component

I don't know actually, how to name this question! I've a JS file in react native that fetches data from server and compares it to local database. i need this to run simultaneously, how can i achieve that? when app starts, that should run, changing…
Ata Mohammadi
  • 3,430
  • 6
  • 41
  • 69
6
votes
1 answer

React router not redirecting on server

I have a high order component that checks if a user is authenticated and if not will redirect to a different url. Its an isomorphic app and this works client-side but if I turn JS off, the server doesn't redirect. if (!this.props.authenticated) { …
Paul
  • 363
  • 5
  • 15
6
votes
2 answers

How to add login authentication and session to ReactJS React Router and Redux with MongoDB + NodeJS Express?

I have a MongoDB + NodeJS Express server, with Webpack running and able to register a user with credentials to the database. I now would like to add authentication and session when the user logs in. I'm looking into Passport.js but can't seem to…
Jo Ko
  • 7,225
  • 15
  • 62
  • 120
6
votes
1 answer

Webpack SCSS Image URL Links Broken on Nested Routes

Here is my directory structure: - public - src - app.js - assets - images - logo-b-green.png - stylesheets - nav.scss And: // webpack.config.js module.exports = { entry: './src/app.js', output: { path: './public', …
Micah
  • 1,676
  • 16
  • 23
6
votes
1 answer

React-router not changing routes with input onBlur event

I'm using React, Redux, and React Router for an application and having trouble with using on onBlur input event in tandem with a route change. I have a "SearchResults" component that presents a list of internal links. SearchResults only shows when…
Himmel
  • 3,629
  • 6
  • 40
  • 77
6
votes
3 answers

Building a SPA with react on top of MVC Routes

I have an API that has routes managed by MVC. On top of that i want to build a SPA with react. However the routes I build from inside my react app cannot be reached, i get an 404 from ISS, here us a stub from my code. export default class Layout…
6
votes
1 answer

using analytics.js and react-ga

I'm setting up Google Analytics for the first time and opted to use react-ga for it's simple integration into react-router and our React components. My question is, do I also need to set up the app with Google's analytics.js, or can I simply use the…
Rachel Lanman
  • 499
  • 1
  • 5
  • 15
6
votes
1 answer

Use absolute path or relative path in React Route

All: I am pretty new to React Router, when I follow its offical tutorial at lesson 7: https://github.com/reactjs/react-router-tutorial/tree/master/lessons/07-more-nesting When it comes to route params like: // index.js // ...
Kuan
  • 11,149
  • 23
  • 93
  • 201
6
votes
2 answers

React router: reset focus on route change (accessibility)

I'm trying to improve accessibility in my existing app and I've noticed that when my route changes the browser focus remains unchanged. My expectation was for the focus to be back in the first element on the page so that I can take advantage of skip…
Alan Souza
  • 7,475
  • 10
  • 46
  • 68
6
votes
2 answers

Redux-saga preload data on route change

I want to load a set of data from an api using redux-saga but I can't find an example of how to do this when navigating to a new route (eg /posts) before rendering the route. How would I do this?
6
votes
3 answers

React update state on click

I have small component which lists data structure as shown below. What i want to do is when i click the update button i want to increment vote key, however, i didn't find a way to do it properly. Do i need to update the whole data state? I have…
agriboz
  • 4,724
  • 4
  • 35
  • 49
6
votes
3 answers

React-router what is $?

I'm trying to figure out if this is a react-router thing or just a React thing. I am talking about the $ here in their example: react-router API {user.name} is the ${} a react thing? …
PositiveGuy
  • 17,621
  • 26
  • 79
  • 138
6
votes
3 answers

how to solve Component should be written as a pure function error in eslint-react?

const Header = React.createClass({ contextTypes: { router: React.PropTypes.object.isRequired, }, render() { return (
  • A …
  • Veljko Simic
    • 73
    • 1
    • 5
    6
    votes
    2 answers

    Using Redux Form with React Router

    I've been spinning my wheels for a while now trying to figure out the best way to do all of this. Here is my situation: I'm using a mix of React Router and Redux Form to create login/create account pages. export default (
    user2465134
    • 8,793
    • 5
    • 32
    • 46
    6
    votes
    2 answers

    React, Redux, React-Router - Stuck at installing material-ui

    Tutorial: However since I am using redux and react router I can not really put MuiThemeProvider to the top of the chain. What is the best way to include this library? That is my ReactDOM.render function: ReactDOM.render(
    Grego
    • 327
    • 7
    • 23