This file contains 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
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* — |
This file contains 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
const createStore = (reducer) => { | |
let state; | |
let listeners = []; | |
const getState = () => state; | |
const dispatch = (action) => { | |
state = reducer(state, action); | |
listeners.forEach(listener => listener()); | |
}; |
This file contains 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
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 |
This file contains 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
/** | |
* 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 |
This file contains 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
/** | |
* 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) |
This file contains 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
plugins: [ | |
new webpack.DefinePlugin({ | |
'process.env': { | |
NODE_ENV: JSON.stringify('production') | |
} | |
}), | |
new webpack.optimize.UglifyJsPlugin() | |
'transform-react-constant-elements', | |
'transform-react-inline-elements', | |
...otherPlugins |
This file contains 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
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()} |
This file contains 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
class CounterButton extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {count: 1}; | |
} | |
shouldComponentUpdate(nextProps, nextState) { | |
if (this.props.color !== nextProps.color) { | |
return true; | |
} |
This file contains 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
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})); | |
} |
This file contains 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
// 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 |
OlderNewer