/* eslint-disable react/forbid-prop-types */ import React from 'react'; import PropTypes from 'prop-types'; import {actions, href, isActive, locationHash, locationHistory} from '../../src'; import {createStore} from './store'; const store = createStore(); if (process.env.HISTORY === 'HASH') { // When publishing to GitHub Pages we cannon use HTML5 history navigation locationHash({store, namespace: 'componentRouter'}); } else { locationHistory({store, namespace: 'componentRouter'}); } const navigateTo = params => event => { event.preventDefault(); store.dispatch(actions.navigateTo(params)); }; const GlobalLinks = ({routingState}) => ( ); GlobalLinks.propTypes = { routingState: PropTypes.object.isRequired }; class ComponentLinks extends React.Component { static propTypes = { routingState: PropTypes.object.isRequired }; componentDidMount() { store.dispatch(actions.addDefaultParam('component', 'baz')); } componentWillUnmount() { store.dispatch(actions.removeParam('component')); } render() { const {routingState} = this.props; return ( component: bla component: baz ); } } class SortedComponentLinks extends React.Component { static propTypes = { routingState: PropTypes.object.isRequired }; componentDidMount() { store.dispatch(actions.addDefaultParam('offRecord', 'bla')); store.dispatch(actions.addOffRecordParam('offRecord')); } componentWillUnmount() { store.dispatch(actions.removeParam('offRecord')); } render() { const {routingState} = this.props; return (

Changes are going to replace browser history

{['bla', 'baz', 'abc', 'zyx'].map(item => ( off-record: {item} ))}
); } } const Header = props => (
); const Foo = props => (

Foo

); const Bar = () => (

Bar

); const CleanHistory = props => (

CleanHistory

); const Home = () => (

Home

); const NotFound = () => (

Not Found

); // First matching route wins, so they should be ordered // from the most specific to the least specific in case of overlap const routes = { '/': Home, '/foo': Foo, '/foo/:*/:something/more': Foo, '/foo/:*/:something': Foo, '/foo/:*': Foo, '/bar/:*': Bar, '/cleanHistory': CleanHistory }; // Add routes Object.keys(routes).forEach(route => store.dispatch(actions.addRoute(route))); class App extends React.Component { state = { routingState: store.getState().componentRouter }; componentDidMount() { this.unsubscribe = store.subscribe( () => this.setState({routingState: store.getState().componentRouter}) ); } componentWillUnmount() { this.unsubscribe(); } render() { const {routingState} = this.state; const CurrentComponent = routes[routingState.currentRoute.route] || NotFound; return (

component-router

Routing state:
{JSON.stringify(routingState, null, 2)}
); } } export default App;