react-router-v4

How to handle Authentication with react-router?

梦想的初衷 提交于 2019-11-30 08:53:08
问题 Trying to make certain routes require Authentication. I have this: class App extends Component { render() { const menuClass = `${this.props.contentMenuClass} col-xs-12 col-md-9`; return ( <BrowserRouter history={browserHistory}> <div className="App"> <Header properties={this.props} /> <div className="container-fluid"> <div className="row"> <SideNav /> <div className={menuClass} id="mainContent"> <Switch> {routes.map(prop => ( <Route path={prop.path} component={prop.component} key={prop.id}

How to rewrite the protected router using TypeScript and React-Router 4?

為{幸葍}努か 提交于 2019-11-30 07:13:24
I was trying to create a <PrivateRoute> as describe in the react-router documents using TypeScript. Can anyone help me out? The privateRoute in react-router document: const PrivateRoute = ({ component: Component, ...rest }) => ( <Route {...rest} render={props => ( fakeAuth.isAuthenticated ? ( <Component {...props}/> ) : ( <Redirect to={{pathname: '/login', state: { from: props.location } }}/> ) )}/> ) Below is my TypeScript version(it won't work) : const PrivateRoute = (theProps: { path: string, component: React.SFC<RouteComponentProps<any> | undefined> | React.ComponentClass

Changing the URL in react-router v4 without using Redirect or Link [duplicate]

风格不统一 提交于 2019-11-30 05:43:27
This question already has an answer here: Programmatically Navigate using react-router 8 answers I'm using react-router v4 and material-ui in my React app. I was wondering how to change the URL once there is a click on a GridTile within GridList . My initial idea was to use a handler for onTouchTap . However, the only way I can see to redirect is by using the components Redirect or Link . How could I change the URL without rendering those two components? I've tried this.context.router.push('/foo') but it doesn't seem to work. Try this, this.props.router.push('/foo') warning works for versions

Create own react route class in typescript

纵然是瞬间 提交于 2019-11-30 05:16:38
I found this ( reacttraining.com ) site, which explains react-router with some examples. But I am not be able to do this with a typescript class. What I want to do is extend the Route class to build my own one. Right now I want to implement it in typescript for authentication as in the following example from the site. const PrivateRoute = ({ component, ...rest }) => ( <Route {...rest} render={props => ( fakeAuth.isAuthenticated ? ( React.createElement(component, props) ) : ( <Redirect to={{ pathname: '/login', state: { from: props.location } }}/> ) )}/> ) I searched a lot, but couldn't find a

How do I create react-router v4 breadcrumbs?

爱⌒轻易说出口 提交于 2019-11-30 03:18:00
How do I create react-router v4 breadcrumbs? I tried asking this question on the react-router V4 website via an issue ticket. They just said to see the recursive paths example. I really want to create it in semantic-ui-react Felipe Taboada I was after the same thing and your question pointed me in the right direction. This worked for me: const Breadcrumbs = (props) => ( <div className="breadcrumbs"> <ul className='container'> <Route path='/:path' component={BreadcrumbsItem} /> </ul> </div> ) const BreadcrumbsItem = ({ match, ...rest }) => ( <span> <li className={match.isExact ? 'breadcrumb

React router v4 not working with Redux

偶尔善良 提交于 2019-11-30 01:49:37
Developing a React application using React router v4. All worked well until I introduced Redux in my app. Since then on click of links to change route the browser url changes but the component corresponding to the route is not getting loaded. It works well if I comment out Redux code. What could be causing this? Here is my code for routing: import React, { Component } from 'react'; import { Switch, Route, Link } from 'react-router-dom'; import LeftSubDefault from './../components/left-sub-default.component'; import LeftSubOne from './../components/left-sub-one.component'; import LeftSubTwo

React Router 4.x - PrivateRoute not working after connecting to Redux

不打扰是莪最后的温柔 提交于 2019-11-30 00:35:06
PrivateRoute available in Example https://reacttraining.com/react-router/web/example/auth-workflow is not working after connecting with Redux. My PrivateRoute look almost same to the original version but only connected to Redux and used it instead of fakeAuth in the original example. const PrivateRoute = ({ component: Component, auth, ...rest }) => ( <Route {...rest} render={props => auth.isAuthenticated ? <Component {...props} /> : <Redirect to={{ pathname: "/login" }} />} /> ); PrivateRoute.propTypes = { auth: PropTypes.object.isRequired, component: PropTypes.func.isRequired } const

How to configure webpack dev server with react router dom v4?

丶灬走出姿态 提交于 2019-11-29 16:14:35
问题 This is the code of my webpack configuration: const compiler = webpack({ entry: ['whatwg-fetch', path.resolve(__dirname, 'js', 'app.js')], module: { loaders: [ { exclude: /node_modules/, test: /\.js$/, loader: 'babel', }, ], }, output: {filename: 'app.js', path: '/'}, }); const app = new WebpackDevServer(compiler, { contentBase: '/public/', proxy: {'/graphql': `http://localhost:${GRAPHQL_PORT}`}, publicPath: '/js/', stats: {colors: true}, }); app.use('/', express.static(path.resolve(__dirname

react router with dynamic routes give 404 on a browser direct link

China☆狼群 提交于 2019-11-29 12:41:46
I have a React Front-end connected to Wordpress API and the routes are defined in clientside with React Router . When i use Link to direct them to dynamic routes they work fine. But when i use browser address bar directly to access the links pressing enter, or i refresh.i get 404 page not found and i got to know that is because a direct call to server is happening and the there's no javascript to manipulate the react-router actions on the shared host when we have a PHP Server on that. Thought about react-snapshot but its not going to suit the dynamic nature from the API.Please can anyone i

React router v4 - Authorized routes with HOC

北城以北 提交于 2019-11-29 12:02:49
I have a problem to prevent unauthorized users from accessing authorized-only routes/components - such as logged in users dashboard I have the following code: import React from 'react' //other imports import {withRouter} from 'react-router' class User extends React.Component { constructor(props) { super(props) console.log('props', props) let user = JSON.parse(localStorage.getItem('userDetails')) if(!user || !user.user || props.match.params.steamId !== user.user.steamId) { props.history.push('/') } else { this.props.updateUserState(user) this.props.getUser(user.user.steamId) } } //render