React-router: Difference between revisions
Jump to navigation
Jump to search
Line 6: | Line 6: | ||
npm i react-router-dom | npm i react-router-dom | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= | ==Add BrowserRouter to Index.js== | ||
<syntaxhighlight lang="js"> | <syntaxhighlight lang="js"> | ||
... | ... | ||
Line 19: | Line 19: | ||
document.getElementById('root') | document.getElementById('root') | ||
); | ); | ||
</syntaxhighlight> | |||
==Create Page== | |||
Create a component for each page | |||
<syntaxhighlight lang="js"> | |||
import React, { Component } from 'react'; | |||
class Profile extends Component { | |||
render() { | |||
return ( | |||
<div> | |||
<h2>Profile</h2> | |||
</div> | |||
); | |||
} | |||
} | |||
export default Profile; | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 23:46, 24 June 2021
Introduction
This page is about routering with React.
Setup
We need to install the pacakge
npm i react-router-dom
Add BrowserRouter to Index.js
...
import { BrowserRouter } from 'react-router-dom';
...
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>,
document.getElementById('root')
);
Create Page
Create a component for each page
import React, { Component } from 'react';
class Profile extends Component {
render() {
return (
<div>
<h2>Profile</h2>
</div>
);
}
}
export default Profile;
Transitions
You need to provide a class prefix in this case trans.
import {TransitionGroup, CSSTransition} from 'react-transition-group'
<TransactionGroup>
<CSSTransition key={location.key} classNames={'trans'} timeout={1000}>
<Switch location={location}>
<Route path={`${match.url}`} component={LoremNumber} exact/>
<Route path={`${match.url}/:id`} component={LoremNumber} exact/>
</Switch>
</TransactionGroup>
And the CSS
.trans-enter {
opacity: 0;
z-index: 1;
}
.trans-exit {
display: none
}
.trans-enter.trans-enter-active {
opacity: 1;
transition: opacity 1000ms ease-in;
}