Skip to content

Instantly share code, notes, and snippets.

@koistya
Last active August 27, 2015 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koistya/613c6ef0c7452cca12a1 to your computer and use it in GitHub Desktop.
Save koistya/613c6ef0c7452cca12a1 to your computer and use it in GitHub Desktop.

/src/core/Location.js

import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
import createHistory from 'history/lib/createBrowserHistory';
import useQueries from 'history/lib/useQueries';

export default canUseDOM ? useQueries(createHistory)({}) : {};

/src/app.js

import ReactDOM from 'react-dom';
import Location from './core/Location';

const routes = {
  '/': async() => <HomePage />,
  '/another': async() => <AnotherPage />
};

Location.listen(location => {
  const component = await routes[location.pathname]();
  ReactDOM.render(component, document.body);
});

/src/components/Link

import React from 'react';
import Location from '../core/Location';

class Link {

  handleClick(event) {
    event.preventDefault();
    Location.pushState(null, event.target.href);
  }

  render() {
    const { children, ...props } = this.props;
    return <a onClick={handleClick} {...props}>{children}</a>;
  }

}

export default Link;

See the full sample in react-starter-kit!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment