Skip to content

Instantly share code, notes, and snippets.

View treyhuffine's full-sized avatar

Trey Huffine treyhuffine

View GitHub Profile
@treyhuffine
treyhuffine / Redux Logic Flow — Crazy Simple Summary
Last active May 30, 2017 13:48
Redux Logic Flow — Crazy Simple Summary
It seems like every blog post about Redux is thousands of words when all that’s needed is a dead simple example of the logic process to refer to. Thus, I created this crazy simple summary. Feel free to update and critique as necessary.
https://github.com/treyhuffine/redux-simple-summary
Redux — Hold and update the entire state of the app in the simplest manner possible while also using the least amount of boilerplate code.
Actions — Pure object that is returned by a pure function with no side-effects. The action object contains the “type” and any necessary information to perform it. It does not actually do anything, it just defines the action and passes the necessary data. The object is sent to the store using dispatch(). Actions describe that something happened.
Reducer — A pure function that takes the current state and action, and performs the update on the state. It returns the new state. Typically use a switch statement that reads the action.type and then creates the new state with this action.
Store* — 
const createStore = (reducer) => {
let state;
let listeners = [];
const getState = () => state;
const dispatch = (action) => {
state = reducer(state, action);
listeners.forEach(listener => listener());
};
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
componentDidCatch(error, info) {
// Display fallback UI
this.setState({ hasError: true });
// You can also log the error to an error reporting service
/**
* Logs all actions and states after they are dispatched.
*/
const logger = store => next => action => {
console.group(action.type)
console.info('dispatching', action)
let result = next(action)
console.log('next state', store.getState())
console.groupEnd(action.type)
return result
/**
* Logs all actions and states after they are dispatched.
*/
const logger = function(store) {
return function(next) {
return function(action) {
console.group(action.type)
console.info('dispatching', action)
let result = next(action)
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.UglifyJsPlugin()
'transform-react-constant-elements',
'transform-react-inline-elements',
...otherPlugins
function ListItem(props) {
// Correct! There is no need to specify the key here:
return <li>{props.value}</li>;
}
function UserList(props) {
const users = props.users;
const listItems = users.map((user) =>
// Correct! Key should be specified inside the array.
<ListItem key={user.id.toString()}
class CounterButton extends React.Component {
constructor(props) {
super(props);
this.state = {count: 1};
}
shouldComponentUpdate(nextProps, nextState) {
if (this.props.color !== nextProps.color) {
return true;
}
class CounterButton extends React.PureComponent {
constructor(props) {
super(props);
this.state = {count: 1};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(state => ({count: state.count + 1}));
}
// Originally inspired by David Walsh (https://davidwalsh.name/javascript-debounce-function)
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// `wait` milliseconds.
const debounce = (func, wait) => {
let timeout;
// This is the function that is returned and will be executed many times
// We spread (...args) to capture any number of parameters we want to pass