Answer the question
In order to leave comments, you need to log in
How to open pages by id in react-router?
How to set up react-router so that you can navigate normally by id
index.js looks like this
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { Route } from 'react-router';
import { BrowserRouter } from 'react-router-dom';
import AuthProvider from './containers/AuthProvider';
import Home from "./component/Home";
import Auth from "./AuthForm";
import Test from './Test';
import { WithHelmet, withHelmet } from './utils/WithHelmet';
....
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<AuthProvider authComponent={withHelmet({ title: 'Авторизация'})(Auth)}>
<Route path='/' strict>
<WithHelmet title='Главная страница'>
<Home />
</WithHelmet>
</Route>
<Route path='/test/:id'>
<WithHelmet title='Test'>
<Test />
</WithHelmet>
</Route>
</AuthProvider>
</BrowserRouter>
</Provider>,
document.getElementById('content')
);
import React from 'react';
import { connect } from 'react-redux';
const AuthProvider = ({ authenticate, authComponent, children}) => {
return authenticate ? children : React.createElement(authComponent);
};
export default connect(state => ({
authenticate: Boolean(state.login.user)
}))(AuthProvider);
import React from 'react';
import Helmet from 'react-helmet';
export const WithHelmet = ({ children, ...helmetProps }) => {
return (
<React.Fragment>
<Helmet>
{helmetProps.title && (
<title>{helmetProps.title}</title>
)}
</Helmet>
{children}
</React.Fragment>
)
};
export const withHelmet = helmetProps => BaseComponent => props => (
<WithHelmet {...helmetProps}><BaseComponent {...props} /></WithHelmet>
);
export default WithHelmet;
<ul>
{this.state.item.map(item => (
<li key={item.id}>
<Link to={`/test/${item.id}`}>{item.name}</Link>
</li>
))}
</ul>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question