Skip to content

Instantly share code, notes, and snippets.

@maddrag0n
Last active February 24, 2018 08:56
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 maddrag0n/4a463b62a109ed4c7930824e51fc8c00 to your computer and use it in GitHub Desktop.
Save maddrag0n/4a463b62a109ed4c7930824e51fc8c00 to your computer and use it in GitHub Desktop.
ReactJS/Redux live boilerplate (single html with prop-types, redux-thunk, redux-logger, Immutable.js, router, react-bootstrap, Axios.js, fetch, lodash, recompose). Created for training purposes. Released under BSD-3-Clause.
<!doctype html>
<html lang="en" itemscope itemtype="http://schema.org/Product">
<head>
<title>ReactJS/Redux Live Boilerplate</title>
<meta charset="utf-8">
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, width=device-width">
<link data-description="bootstrap styles" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="http://unpkg.com/babel-standalone@6/babel.min.js">/* <script type='text/babel'> */</script>
<script src="http://unpkg.com/react@latest/umd/react.development.js">/* exposes React */</script>
<script src="http://unpkg.com/react-dom@latest/umd/react-dom.development.js">/* exposes ReactDOM */</script>
<script src="https://unpkg.com/react-router-dom@latest/umd/react-router-dom.min.js">/* exposes ReactRouterDom */</script>
<script src="https://unpkg.com/prop-types@latest/prop-types.min.js">/* exposes PropTypes */</script>
<script src="https://unpkg.com/react-redux@latest/dist/react-redux.min.js">/* exposes ReactRedux */</script>
<script src="https://unpkg.com/redux@latest/dist/redux.min.js">/* exposes Redux */</script>
<script src="https://unpkg.com/redux-thunk@latest/dist/redux-thunk.min.js">/* exposes ReduxThunk */</script>
<script src="https://unpkg.com/redux-logger@latest/dist/redux-logger.js">/* exposes ReduxLogger */</script>
<script src="https://unpkg.com/immutable@latest/dist/immutable.js">/* exposes Immutable */</script>
<script src="https://unpkg.com/react-bootstrap@latest/dist/react-bootstrap.min.js">/* exposes ReactBootstrap */</script>
<script src="https://unpkg.com/lodash@latest/lodash.min.js">/* exposes _ */</script>
<script src="https://unpkg.com/whatwg-fetch@latest/fetch.js">/* fetch polyfill, exposes fetch */</script>
<script src="https://unpkg.com/axios@latest/dist/axios.min.js">/* exposes axios */</script>
<script src="https://unpkg.com/recompose@latest/build/Recompose.min.js">/* exposes Recompose */</script>
</head>
<body>
<script type='text/babel'>
/**
* The following list of globals are available
* without any further action. The list is for your reference
* and has no other function.
*
* **React**
* @global React [ReactJS](https://reactjs.org/docs/react-api.html)
* @global ReactDOM [ReactDOM](https://reactjs.org/docs/react-dom.html)
* @global PropTypes [prop-types component props checking](https://reactjs.org/docs/typechecking-with-proptypes.html)
* @global ReactRouterDom [react-router](https://reacttraining.com/react-router/web/api/HashRouter)
*
* **Redux**
* @global Redux [Redux (flux implementation)](https://redux.js.org/api-reference)
* @global ReactRedux [Rect bindings for Redux](https://redux.js.org/basics/usage-with-react)
* @global ReduxThunk [async action creators](https://redux.js.org/advanced/async-actions#async-action-creators)
* @global reduxLogger [redux-logger reducer logging](https://github.com/evgenyrodionov/redux-logger)
*
* **Tools**
* @global Immutable [Immutable.js (immutable data structures)](http://facebook.github.io/immutable-js/docs/#/)
* @global _ [Lodash (js utilities)](https://lodash.com/docs/)
* @global fetch [Fetch API (http client)](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch)
* @global axios [Axios.js (http client)](https://github.com/axios/axios)
* @global Recompose [Recompose (react utilities)](https://github.com/acdlite/recompose/blob/master/docs/API.md)
*
* **View libraries**
* @global ReactBootstrap [react-bootstrap](https://react-bootstrap.github.io/getting-started/introduction)
*
*/
const RBS = ReactBootstrap;
const Router = ReactRouterDOM;
/**
* redux logger with support for immutables
*/
const logger = reduxLogger.createLogger({
stateTransformer: (state) => {
const newState = {};
Object.keys(state).forEach((i) => {
if (Immutable.Iterable.isIterable(state[i])) newState[i] = state[i].toJS();
else newState[i] = state[i];
});
return newState;
},
});
// REDUCERS
const appState = (state, action) => {
if (!state) state = Immutable.Map();
const { type, payload } = action;
switch(type) {
case 'SAVE_USER':
return state.set('user', Immutable.fromJS(payload));
case 'DELETE_USER':
return state.delete('user');
case 'UPDATE_USER':
return state.set('user', Immutable.merge(state.get('user'), Immutable.fromJS(payload)));
}
return state;
}
/**
* create store
*/
const store = Redux.createStore(
Redux.combineReducers({
appState,
}),
Redux.applyMiddleware(ReduxThunk.default, logger)
);
/**
* Your main container
* @extends React
*/
class App extends React.Component {
render() {
return (
<ReactRedux.Provider store={store}>
<Router.HashRouter>
<div style={{ padding: 24 }}>
{this.renderNavigation()}
<Router.Switch>
<Router.Route exact path='/'>
<span>Home</span>
</Router.Route>
<Router.Route exact path='/page-1'>
<span>Page 1</span>
</Router.Route>
<Router.Route exact path='/page-2'>
<span>Page 2</span>
</Router.Route>
</Router.Switch>
</div>
</Router.HashRouter>
</ReactRedux.Provider>
);
}
renderNavigation = () => (
<RBS.Navbar inverse>
<RBS.Navbar.Header>
<RBS.Navbar.Brand>
<a href='#/'>Redux/ReactJS Live Boilerplate</a>
</RBS.Navbar.Brand>
</RBS.Navbar.Header>
<RBS.Navbar.Collapse sm={1}>
<RBS.Navbar.Text>
<small>by <a href='https://github.com/maddrag0n'>maddrag0n</a></small>
</RBS.Navbar.Text>
<RBS.Nav>
<RBS.NavItem href='#/page-1'>
Page 1
</RBS.NavItem>
<RBS.NavItem href='#/page-2'>
Page 2
</RBS.NavItem>
</RBS.Nav>
</RBS.Navbar.Collapse>
</RBS.Navbar>
);
}
/**
* this part renders your react app into the DOM
* @param {ReactElement} the react app to render
*/
((reactAppElement) => {
const domTarget = document.getElementsByTagName('body')[0].appendChild(document.createElement('react-app'));
domTarget.setAttribute('description', 'this is your ReactDOM render target');
ReactDOM.render(
reactAppElement,
domTarget,
);
})(<App/>);
</script>
</body>
</html>
<!--
BSD-3-Clause
Copyright 2018 Josh Li
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment