Score
0
Watch 2 Star 5 Fork 0

wengcd / react-start-kit-flux-demoJavaScriptMIT

Join us
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
使用react-start-kit+flux 创建的获取多条数据demo spread retract

Clone or download
how-to-implement-routing.md 4.01 KB
Copy Edit Web IDE Raw Blame History

How to Implement Routing and Navigation img

Step 1: Basic Routing

In its simplest form the routing looks like a collection or URLs where each URL is mapped to a React component:

// app.js
import React from 'react';
import Layout from './components/Layout';
import HomePage from './components/HomePage';
import AboutPage from './components/AboutPage';
import NotFoundPage from './components/NotFoundPage';
import ErrorPage from './components/ErrorPage';

const routes = {
  '/':      <Layout><HomePage /></Layout>,
  '/about': <Layout><AboutPage /></Layout>
};

const container = document.getElementById('app');

function render() {
  try {
    const path = window.location.hash.substr(1) || '/';
    const component = routes[path] || <NotFoundPage />;
    React.render(component, container);
  } catch (err) {
    React.render(<ErrorPage {...err} />, container);
  }
}

window.addEventListener('hashchange', () => render());
render();

Step 2: Asynchronous Routes

Just wrap React components inside your routes into asynchronous functions:

import React from 'react';
import http from './core/http';
import Layout from './components/Layout';
import HomePage from './components/HomePage';
import AboutPage from './components/AboutPage';
import NotFoundPage from './components/NotFoundPage';
import ErrorPage from './components/ErrorPage';

const routes = {
  '/': async () => {
    const data = await http.get('/api/data/home');
    return <Layout><HomePage {...data} /></Layout>
  },
  '/about': async () => {
    const data = await http.get('/api/data/about');
    return <Layout><AboutPage {...data} /></Layout>;
  }
};

const container = document.getElementById('app');

async function render() {
  try {
    const path = window.location.hash.substr(1) || '/';
    const route = routes[path];
    const component = route ? await route() : <NotFoundPage />;
    React.render(component, container);
  } catch (err) {
    React.render(<ErrorPage {...err} />, container);
  }
}

window.addEventListener('hashchange', () => render());
render();

Step 3: Parameterized Routes

(1) Convert the list of routes from hash table to an array, this way the order of routes will be preserved. (2) Wrap this collection into a Router class, where you can put .match(url) async method. (3) Use path-to-regexp to convert Express-like path strings into regular expressions which are used for matching URL paths to React components.

import React from 'react';
import Router from 'react-routing/src/Router';
import http from './core/http';
import Layout from './components/Layout';
import ProductListing from './components/ProductListing';
import ProductInfo from './components/ProductInfo';
import NotFoundPage from './components/NotFoundPage';
import ErrorPage from './components/ErrorPage';

const router = new Router(on => {
  on('/products', async () => {
    const data = await http.get('/api/products');
    return <Layout><ProductListing {...data} /></Layout>
  });
  on('/products/:id', async (req) => {
    const data = await http.get(`/api/products/${req.params.id}`);
    return <Layout><ProductInfo {...data} /></Layout>;
  });
}]);

const container = document.getElementById('app');

async function render() {
  const state = { path: window.location.hash.substr(1) || '/' };
  await router.dispatch(state, component => {
    React.render(component, container);
  });
}

window.addEventListener('hashchange', () => render());
render();

Step 4. Handling Redirects

Coming soon. Stay tuned!

Comment ( 0 )

Sign in for post a comment

JavaScript
1
https://gitee.com/wengcd/react-start-kit-flux-demo.git
git@gitee.com:wengcd/react-start-kit-flux-demo.git
wengcd
react-start-kit-flux-demo
react-start-kit-flux-demo
master

Help Search