Last active
February 5, 2017 19:16
-
-
Save DreySkee/1066b7b250bfce4a9e96a2acffdeb04c to your computer and use it in GitHub Desktop.
14 - Wordpress API + ReactJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import {render} from 'react-dom'; | |
import App from './components/App.js'; | |
import Home from './components/Home.js'; | |
import views from './components/views.js'; | |
import { | |
browserHistory, | |
IndexRoute, | |
Redirect, | |
Route, | |
Router | |
} from 'react-router'; | |
import DataActions from './actions/DataActions.js'; | |
class AppInitializer { | |
buildRoutes(data) { | |
return data.pages.map((page, i) => { | |
const component = views[page.slug]; | |
return ( | |
<Route | |
getComponent={(nextState, cb) => { | |
require.ensure([], (require) => { | |
cb(null, require(component).default); | |
}); | |
}} | |
key={ page.id } | |
path={`/${page.slug}`} | |
/> | |
); | |
}); | |
} | |
run() { | |
DataActions.getPages((response)=>{ | |
render( | |
<Router history={browserHistory}> | |
<Route path="/" component={ App } > | |
<IndexRoute component={ Home } /> | |
{this.buildRoutes(response)} | |
</Route> | |
<Redirect from="*" to="/" /> | |
</Router> | |
, document.getElementById('app') | |
); | |
}); | |
} | |
} | |
new AppInitializer().run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment