Our speaker of the evening was Kristof Van Miegem, co-founder of Codifly (http://codifly.be/). He explained how to manage application state in React applications with Redux, followed by a focus on immutable data structures and how these data structures fit perfectly into this story.
10. State is Read-Only
The only way to change the state is to emit an
Action:
• A plain JavaScript that describes what
happened.
• Expresses an intent to change state
15. State Changes are Made with Pure Functions
To specify how the state tree is transformed by
actions, you write reducers:
• Pure functions
• that take the previous state and an
action, and return the next state
18. Bringing it All Together: The Store
The store is responsible for:
• Containing the current state of the app
• Allowing state updates
• Notification of listeners
19. Store creation
let store = createStore(reducer)
Triggering changes: dispatching actions
store.dispatch(actionCreator(…))
Listening for state changes
store.subscribe(() =>
console.log(store.getState())
)
Bringing it All Together: The Store
20. Full Example
// Actions
const inc = () => ({ type: 'INC' });
const dec = () => ({ type: 'DEC' });
// Reducer
const counter = (state = 0, action) => {
switch (action.type) {
case ‘INC': return state + 1;
case ‘DEC': return state - 1;
default: return state;
}
};
21. Full Example
// View
class Counter extends React.Component {
render () {
return (
<div>
<h1>{this.props.value}</h1>
<button onClick={this.props.onIncrement}>
+
</button>
<button onClick={this.props.onDecrement}>
-
</button>
</div>
);
}
}
22. Full Example
// Store
const store = Redux.createStore(counter);
// Rendering
const render = () => {
ReactDOM.render(
<Counter
value={store.getState()}
onIncrement={() => store.dispatch(inc())}
onDecrement={() => store.dispatch(dec())}
/>,
document.getElementById('container')
);
};
render();
store.subscribe(render); https://jsfiddle.net/pxktc6pv/
31. Immutable List
var a = [3]i;
var a2
= a.push(4);
console.log(a);
console.log(a2);
var l = List([3]);
var l2
= list.push(4);
console.log(l.toJS());
console.log(l2.toJS());
32. Updating Immutable Data
(1) Deep clone
(2) Perform changes
(3) Return result
Conceptually, the update process
40. “EditorConfig helps developers define and
maintain consistent coding styles between
different editors and IDEs.”
http://editorconfig.org/
Editorconfig
42. function gcd (a, b) {
if (a == 0)
return b;
else {
return gcd(b % a, a);
}
}
ESLint
43. function gcd (a, b) {
if (a == 0)
return b;
else {
return gcd(b % a, a);
}
}
ESLint
2:3 error Expected { after 'if' condition
2:9 error Expected '===' and instead saw '=='
4:8 warning Unexpected 'else' after 'return'
8:1 warning Block must not be padded by blank lines
44. function gcd (a, b) {
if (a === 0) {
return b;
}
return gcd(b % a, a);
}
ESLint