SlideShare a Scribd company logo
3
Most read
7
Most read
11
Most read
Front End Workshops
React Router
Mario García Martín
mgarcia@visual-engin.com
React Router
Client side navigation
Introduction
Inspired by Ember.js router
React-router
React
History
How does a router work?
*See alternative data management libraries at https://github.com/facebook/react/wiki/Complementary-Tools#model-management
*See alternative routing libraries at https://github.com/facebook/react/wiki/Complementary-Tools#routing
Histories
A history knows how to listen to the browser’s address bar for changes
Different flavors are available...
...but you can build your custom history (if you dare)!
http://example.com/some/path
http://example.com/#/some/path
browserHistory
createMemoryHistory
hashHistory
Routing basics (1 of 2)
ReactDOM.render(
<App />,
document.querySelector('.container')
);
From... to...
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" component={App} />
</Router>,
document.querySelector('.container')
);
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" component={App} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
<Route path="*" component={NotFound} />
</Router>,
document.querySelector('.container')
);
A more practical example...
Routing basics (2 of 2)
Same example using the routes prop...
import routes from './routes';
ReactDOM.render(
<Router history={browserHistory} routes={routes} />,
document.querySelector('.container')
);
// routes.js
export default (
<Route path="/" component={App} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
<Route path="*" component={NotFound} />
);
A more scalable way of specifying the routes
*For more advanced usage, see https://github.com/reactjs/react-router/blob/master/docs/guides/DynamicRouting.md
Route nesting
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRoute component={Home} />
<Redirect from="home" to="/" />
<Route path="contact" component={Contact} />
<Route path="*" component={NotFound} />
</Route>
</Router>,
document.querySelector('.container')
);
/
App
Home
Don’t forget to
render the children
components inside
their parent!
/home
App
Home
/contact
App
Contact
/random
App
NotFound
{this.props.children}
Dynamic routing
In the component you can use this.props.params.<param_name>
In the route you can define dynamic segments
In the component you can use this.props.params.<param_name>
Define an optional param by wrapping it between parenthesis
<Route path="/users/:userId" component={UserItem} />
<Route path="/contact(/:mode)" component={Contact} />
Navigation links
Link
<Link to="/contact">Contact us</Link>
In the component you can use this.props.location.query.<param_name>
Query params
<Link to={"/users/" + user.id} activeStyle={{ color: red }}>User details</Link>
<Link to={"/users/" + user.id} activeClassName="active">User details</Link>
IndexLink
<IndexLink to="/">Contact us</IndexLink>
<Link to={{ pathname: "/user/bob", query: { showAge: true } }}>Bob</Link>
Navigation within components
The router instance can be made accessible through the contextTypes
class CoolButton extends Component {
static contextTypes = { router: PropTypes.object };
onButtonClick() {
this.context.router.push('/cool');
}
render() {
return <button onClick={this.onButtonClick.bind(this)}>Navigate</button>;
}
}
Invoke router push(‘<target_url>’) or replace(‘<target_url>’)
methods to navigate programmatically
Not the recommended way since react-router 2.4.0*
*See https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#changes-to-thiscontext
Thanks for your time!
Do you have any questions?
Workshop 21: React Router

More Related Content

What's hot (20)

PPTX
What is component in reactjs
manojbkalla
 
PDF
An introduction to React.js
Emanuele DelBono
 
PDF
React js
Rajesh Kolla
 
PPTX
Introduction to React JS for beginners
Varun Raj
 
PDF
React Router: React Meetup XXL
Rob Gietema
 
PPTX
React JS: A Secret Preview
valuebound
 
PDF
ReactJS presentation
Thanh Tuong
 
PDF
React and redux
Mystic Coders, LLC
 
PPTX
Its time to React.js
Ritesh Mehrotra
 
PPTX
Introduction to React
Rob Quick
 
PPTX
Reactjs
Neha Sharma
 
PPTX
Introduction to React JS for beginners | Namespace IT
namespaceit
 
PPTX
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
ODP
Introduction to ReactJS
Knoldus Inc.
 
PDF
Introduction to React JS
Bethmi Gunasekara
 
PDF
Introduction to Redux
Ignacio Martín
 
PPTX
React workshop
Imran Sayed
 
PPTX
React state
Ducat
 
PPTX
Intro to React
Eric Westfall
 
PPTX
Intro to React
Justin Reock
 
What is component in reactjs
manojbkalla
 
An introduction to React.js
Emanuele DelBono
 
React js
Rajesh Kolla
 
Introduction to React JS for beginners
Varun Raj
 
React Router: React Meetup XXL
Rob Gietema
 
React JS: A Secret Preview
valuebound
 
ReactJS presentation
Thanh Tuong
 
React and redux
Mystic Coders, LLC
 
Its time to React.js
Ritesh Mehrotra
 
Introduction to React
Rob Quick
 
Reactjs
Neha Sharma
 
Introduction to React JS for beginners | Namespace IT
namespaceit
 
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
Introduction to ReactJS
Knoldus Inc.
 
Introduction to React JS
Bethmi Gunasekara
 
Introduction to Redux
Ignacio Martín
 
React workshop
Imran Sayed
 
React state
Ducat
 
Intro to React
Eric Westfall
 
Intro to React
Justin Reock
 

Viewers also liked (11)

PDF
Workshop 22: React-Redux Middleware
Visual Engineering
 
PDF
Workshop 20: ReactJS Part II Flux Pattern & Redux
Visual Engineering
 
PDF
Workshop 23: ReactJS, React & Redux testing
Visual Engineering
 
PDF
Webpack & React Performance in 16+ Steps
Grgur Grisogono
 
PDF
Game of Frameworks - GDG Cáceres #CodeCC
Carlos Azaustre
 
PDF
Workshop 24: React Native Introduction
Visual Engineering
 
PDF
Workhop iOS 1: Fundamentos de Swift
Visual Engineering
 
PDF
Workshop 25: React Native - Components
Visual Engineering
 
PDF
Workshop 26: React Native - The Native Side
Visual Engineering
 
PDF
Workshop iOS 2: Swift - Structures
Visual Engineering
 
PPTX
React and Flux life cycle with JSX, React Router and Jest Unit Testing
Eswara Kumar Palakollu
 
Workshop 22: React-Redux Middleware
Visual Engineering
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Visual Engineering
 
Workshop 23: ReactJS, React & Redux testing
Visual Engineering
 
Webpack & React Performance in 16+ Steps
Grgur Grisogono
 
Game of Frameworks - GDG Cáceres #CodeCC
Carlos Azaustre
 
Workshop 24: React Native Introduction
Visual Engineering
 
Workhop iOS 1: Fundamentos de Swift
Visual Engineering
 
Workshop 25: React Native - Components
Visual Engineering
 
Workshop 26: React Native - The Native Side
Visual Engineering
 
Workshop iOS 2: Swift - Structures
Visual Engineering
 
React and Flux life cycle with JSX, React Router and Jest Unit Testing
Eswara Kumar Palakollu
 
Ad

Similar to Workshop 21: React Router (20)

PPTX
reactjs-reactrouter-160711155618 (1).pptx
zmulani8
 
PPTX
reactjs-reactrouter-1607111dfdfdf55618.pptx
zmulani8
 
PDF
Learn more about React Router & it's properties
React Masters
 
PDF
React Router Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
React Router Dom Integration Tutorial for Developers
Inexture Solutions
 
PPTX
React JS CONCEPT AND DETAILED EXPLANATION
harshavardhanjuttika
 
PPTX
How to navigate programmatically using react router
BOSC Tech Labs
 
PDF
Routing in NEXTJS.pdf
AnishaDahal5
 
PDF
Guide to Using React Router V6 in React Apps.pdf
AdarshMathuri
 
PPTX
Up and Running with ReactJS
Loc Nguyen
 
PPTX
Getting into ember.js
reybango
 
PDF
Static sites with react
Robert Pearce
 
PDF
Frontend developer Roadmap .pdf
DurgeshSinghLodhi1
 
PDF
Learn reactjs, how to code with example and general understanding thinkwik
Hetaxi patel
 
DOCX
Vi INFOTECH react syllabus
ViINFOTECH
 
PDF
ReactDC Intro to NextJS 9
Allison Kunz
 
PPTX
React Workshop
GDSC UofT Mississauga
 
PDF
The complete-beginners-guide-to-react dyrr
AfreenK
 
PDF
Full Stack Developer
Akbar Uddin
 
PPTX
Ember Tech Talk
Suchita Doshi
 
reactjs-reactrouter-160711155618 (1).pptx
zmulani8
 
reactjs-reactrouter-1607111dfdfdf55618.pptx
zmulani8
 
Learn more about React Router & it's properties
React Masters
 
React Router Interview Questions PDF By ScholarHat
Scholarhat
 
React Router Dom Integration Tutorial for Developers
Inexture Solutions
 
React JS CONCEPT AND DETAILED EXPLANATION
harshavardhanjuttika
 
How to navigate programmatically using react router
BOSC Tech Labs
 
Routing in NEXTJS.pdf
AnishaDahal5
 
Guide to Using React Router V6 in React Apps.pdf
AdarshMathuri
 
Up and Running with ReactJS
Loc Nguyen
 
Getting into ember.js
reybango
 
Static sites with react
Robert Pearce
 
Frontend developer Roadmap .pdf
DurgeshSinghLodhi1
 
Learn reactjs, how to code with example and general understanding thinkwik
Hetaxi patel
 
Vi INFOTECH react syllabus
ViINFOTECH
 
ReactDC Intro to NextJS 9
Allison Kunz
 
React Workshop
GDSC UofT Mississauga
 
The complete-beginners-guide-to-react dyrr
AfreenK
 
Full Stack Developer
Akbar Uddin
 
Ember Tech Talk
Suchita Doshi
 
Ad

More from Visual Engineering (20)

PDF
Workshop 27: Isomorphic web apps with ReactJS
Visual Engineering
 
PDF
Workshop iOS 4: Closures, generics & operators
Visual Engineering
 
PDF
Workshop iOS 3: Testing, protocolos y extensiones
Visual Engineering
 
PDF
Workshop 22: ReactJS Redux Advanced
Visual Engineering
 
PDF
Workshop 19: ReactJS Introduction
Visual Engineering
 
PDF
Workshop 18: CSS Animations & cool effects
Visual Engineering
 
PDF
Workshop 17: EmberJS parte II
Visual Engineering
 
PDF
Workshop 16: EmberJS Parte I
Visual Engineering
 
PDF
Workshop 15: Ionic framework
Visual Engineering
 
PDF
Workshop 14: AngularJS Parte III
Visual Engineering
 
PDF
Workshop 13: AngularJS Parte II
Visual Engineering
 
PDF
Workshop 8: Templating: Handlebars, DustJS
Visual Engineering
 
PDF
Workshop 12: AngularJS Parte I
Visual Engineering
 
PDF
Workshop 11: Trendy web designs & prototyping
Visual Engineering
 
PDF
Workshop 10: ECMAScript 6
Visual Engineering
 
PDF
Workshop 9: BackboneJS y patrones MVC
Visual Engineering
 
PDF
Workshop 7: Single Page Applications
Visual Engineering
 
PDF
Workshop 6: Designer tools
Visual Engineering
 
PDF
Workshop 5: JavaScript testing
Visual Engineering
 
PDF
Workshop 4: NodeJS. Express Framework & MongoDB.
Visual Engineering
 
Workshop 27: Isomorphic web apps with ReactJS
Visual Engineering
 
Workshop iOS 4: Closures, generics & operators
Visual Engineering
 
Workshop iOS 3: Testing, protocolos y extensiones
Visual Engineering
 
Workshop 22: ReactJS Redux Advanced
Visual Engineering
 
Workshop 19: ReactJS Introduction
Visual Engineering
 
Workshop 18: CSS Animations & cool effects
Visual Engineering
 
Workshop 17: EmberJS parte II
Visual Engineering
 
Workshop 16: EmberJS Parte I
Visual Engineering
 
Workshop 15: Ionic framework
Visual Engineering
 
Workshop 14: AngularJS Parte III
Visual Engineering
 
Workshop 13: AngularJS Parte II
Visual Engineering
 
Workshop 8: Templating: Handlebars, DustJS
Visual Engineering
 
Workshop 12: AngularJS Parte I
Visual Engineering
 
Workshop 11: Trendy web designs & prototyping
Visual Engineering
 
Workshop 10: ECMAScript 6
Visual Engineering
 
Workshop 9: BackboneJS y patrones MVC
Visual Engineering
 
Workshop 7: Single Page Applications
Visual Engineering
 
Workshop 6: Designer tools
Visual Engineering
 
Workshop 5: JavaScript testing
Visual Engineering
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Visual Engineering
 

Recently uploaded (20)

PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPT
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Executive Business Intelligence Dashboards
vandeslie24
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Import Data Form Excel to Tally Services
Tally xperts
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Human Resources Information System (HRIS)
Amity University, Patna
 
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 

Workshop 21: React Router

  • 1. Front End Workshops React Router Mario García Martín [email protected]
  • 3. Introduction Inspired by Ember.js router React-router React History How does a router work? *See alternative data management libraries at https://github.com/facebook/react/wiki/Complementary-Tools#model-management *See alternative routing libraries at https://github.com/facebook/react/wiki/Complementary-Tools#routing
  • 4. Histories A history knows how to listen to the browser’s address bar for changes Different flavors are available... ...but you can build your custom history (if you dare)! http://example.com/some/path http://example.com/#/some/path browserHistory createMemoryHistory hashHistory
  • 5. Routing basics (1 of 2) ReactDOM.render( <App />, document.querySelector('.container') ); From... to... ReactDOM.render( <Router history={browserHistory}> <Route path="/" component={App} /> </Router>, document.querySelector('.container') ); ReactDOM.render( <Router history={browserHistory}> <Route path="/" component={App} /> <Route path="/about" component={About} /> <Route path="/contact" component={Contact} /> <Route path="*" component={NotFound} /> </Router>, document.querySelector('.container') ); A more practical example...
  • 6. Routing basics (2 of 2) Same example using the routes prop... import routes from './routes'; ReactDOM.render( <Router history={browserHistory} routes={routes} />, document.querySelector('.container') ); // routes.js export default ( <Route path="/" component={App} /> <Route path="/about" component={About} /> <Route path="/contact" component={Contact} /> <Route path="*" component={NotFound} /> ); A more scalable way of specifying the routes *For more advanced usage, see https://github.com/reactjs/react-router/blob/master/docs/guides/DynamicRouting.md
  • 7. Route nesting ReactDOM.render( <Router history={browserHistory}> <Route path="/" component={App}> <IndexRoute component={Home} /> <Redirect from="home" to="/" /> <Route path="contact" component={Contact} /> <Route path="*" component={NotFound} /> </Route> </Router>, document.querySelector('.container') ); / App Home Don’t forget to render the children components inside their parent! /home App Home /contact App Contact /random App NotFound {this.props.children}
  • 8. Dynamic routing In the component you can use this.props.params.<param_name> In the route you can define dynamic segments In the component you can use this.props.params.<param_name> Define an optional param by wrapping it between parenthesis <Route path="/users/:userId" component={UserItem} /> <Route path="/contact(/:mode)" component={Contact} />
  • 9. Navigation links Link <Link to="/contact">Contact us</Link> In the component you can use this.props.location.query.<param_name> Query params <Link to={"/users/" + user.id} activeStyle={{ color: red }}>User details</Link> <Link to={"/users/" + user.id} activeClassName="active">User details</Link> IndexLink <IndexLink to="/">Contact us</IndexLink> <Link to={{ pathname: "/user/bob", query: { showAge: true } }}>Bob</Link>
  • 10. Navigation within components The router instance can be made accessible through the contextTypes class CoolButton extends Component { static contextTypes = { router: PropTypes.object }; onButtonClick() { this.context.router.push('/cool'); } render() { return <button onClick={this.onButtonClick.bind(this)}>Navigate</button>; } } Invoke router push(‘<target_url>’) or replace(‘<target_url>’) methods to navigate programmatically Not the recommended way since react-router 2.4.0* *See https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#changes-to-thiscontext
  • 11. Thanks for your time! Do you have any questions?