react-router-v4

React router Switch behavior

随声附和 提交于 2019-12-03 08:22:04
问题 ( react-router-dom version: 4.1.1) I have working routes set up, but I'm a bit confused about why the <Switch> was necessary: index.js import React from 'react'; import { HashRouter, Route, Switch } from 'react-router-dom'; import App from './components/App.jsx'; import FridgePage from './components/FridgePage.jsx'; ReactDOM.render( <HashRouter> <Switch> <Route exact path="/" component={App} /> <Route path="/fridge" component={FridgePage} /> </Switch> </HashRouter>, document.getElementById(

React-router-dom v4 nested routes not working

蹲街弑〆低调 提交于 2019-12-03 03:43:35
In reference to the unresolved question (as a final conclusion) Multiple Nested Routes in react-router-dom v4 How to nest routes in React Router v4? I am also getting the same issue. https://reacttraining.com/react-router/web/guides/quick-start promotes react-router-dom Also, people find better to list down routes in one file rather inside components. Something referred: https://github.com/ReactTraining/react-router/tree/master/packages/react-router-config Something working (mostly): import * as React from 'react' import {BrowserRouter as Router, Route, Switch } from 'react-router-dom' export

React-Router v4 - Link vs Redirect vs History

旧城冷巷雨未停 提交于 2019-12-03 03:13:47
问题 There seems to be some confusion with what to use over the other: <Link to='/some/path'> <Redirect to='/some/path'/> history.push('/some/path') I have been using React/Router for a little while now, and different posts/answers say different things regarding when to use these, and sometimes they don't line up with what someone else said. So I think I need some clarification on this. From what I understand about Link and this documentation it: Provides declarative, accessible navigation around

Authenticate async with react-router-v4

北城以北 提交于 2019-12-03 02:43:31
I have this PrivateRoute component (from the docs): const PrivateRoute = ({ component: Component, ...rest }) => ( <Route {...rest} render={props => ( isAuthenticated ? ( <Component {...props}/> ) : ( <Redirect to={{ pathname: '/login', state: { from: props.location } }}/> ) )}/> ) I would like to change isAuthenticated to an aysnc request isAuthenticated() . However, before the response has returned the page redirects. To clarify, the isAuthenticated function is already set up. How can I wait for the async call to complete before deciding what to display? If you aren't using Redux or any other

Where's hashHistory in React Router v4?

女生的网名这么多〃 提交于 2019-12-03 01:05:34
I'm trying to use a router for my React application. I tried something I'd been using a while back, but can't seem to get it going. Has hashHistory been removed/reformatted in React Router v4? <Router history={hashHistory}> <Route path='/' component={MainContainer} /> </Router> Use a HashRouter . They got rid of individual histories such as browserHistory and hashHistory and instead replaced them with BrowserRouter and HashRouter components respectively in React Router v4: import { HashRouter } from 'react-router-dom'; <HashRouter> … </HashRouter> Note that HashRouter comes from react-router

Advantages of dynamic vs static routing in React

。_饼干妹妹 提交于 2019-12-02 18:14:31
I'm reading about static vs dynamic routing in React Router, and I'm struggling to identify the advantages of the latter (and why v4 chose to go with it). I can see the advantage of listing out all the routes for an application (static), as well as the component that each route maps to, allowing you to trace what would be rendered given a specific URL. But I'm not seeing any clear advantage to dynamic routes. If anything, I can only see disadvantages, because there is no clear way to see what state a URL will map to, without starting at the root app element and working your way through the

Using React IndexRoute in react-router v4

妖精的绣舞 提交于 2019-12-02 14:37:15
I'm learning React myself with online tutorial. So this is a basic example about using React Router: <Router history={browserHistory}> <Route path="/" component={App}> <IndexRoute component={Home}/> <Route path="/home" component={Home}/> <Route path="/about" component={About}/> <Route path="/contact" component={Contact}/> </Route> </Router> With my App component: class App extends React.Component { render() { return ( <div> <ul> <li><Link to="home">Home</Link></li> <li><Link to="about">About</Link></li> <li><Link to="contact">Contact</Link></li> </ul> {this.props.children} </div> ) } } export

React routing, path not working, bundle not found

醉酒当歌 提交于 2019-12-02 06:25:52
The following route works great and displays the AdministratePage component: <Route path="/admin" component={AdministratePage} /> However this route: <Route path="/admin/all" component={AdministratePage} /> ...results in the following error: http://localhost:8080/admin/bundle.js 404 (Not Found) Where do I go wrong? Am I not allowed to use any path? I'm using react-router-dom 4.1.2. My webpack.config.js : output: { path: path.resolve('dist'), filename: '/bundle.js' }, My index.html: <div id="app"></div> Thank you. Slash looks useless here filename: '/bundle.js' Also try to define publicPath

React-Router 4 catch all route

折月煮酒 提交于 2019-12-02 04:39:52
问题 My React routes are defined as follows: ... <Route component={Header} /> <Route exact path="/" component={Landing} /> <Route path="/about" component={About} /> <Route path="/contact" component={Contact} /> <Route component={Footer} /> ... I want to define a catch all route that catches anything that does not match the Landing, About or Contact routes, and instead redirects to a 404 Not found page. How can I do it with React Router 4? 回答1: Try this implementation const AppRouter = (props) => {

Render async component in React Router v4

ε祈祈猫儿з 提交于 2019-12-01 19:38:57
问题 I want to render a component based on the payload it receives from an api like below <Route path="/foo/bar" render={() => { return (get('/some/api').then((res) => { return <Baz data={res.data} /> }).catch((err) => console.log)) }} /> But I'm getting the error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead. even when I do wrap Baz in [] 回答1: unfortunately with the latest version of React - 16. Async