Rob Ocel - Developer, PointSource - @robocell
In 20 Minutes Or Less
Never heard of or used state management patterns?
Using a state management library but feeling confused?
Are you a state management expert?
Data down; events up
function BoilingVerdict(props) {
if (props.celsius >= 100) {
return Water would boil.
;
}
return Water would not boil.
;
}
Vue.component("button-counter", {
template: ``,
props: ["amount"],
data: () => ({
counter: 0
}),
methods: {
incrementCounter: function() {
this.counter += this.amount;
}
}
});
Vue.component("button-counter", {
template: ``,
props: ["counter", "amount"],
methods: {
incrementCounter: function() {
this.$emit("increment", this.amount);
}
}
});
What happens when our tree gets larger?
What happens if we had more state to share?
{
selectedSubreddit: 'frontend',
postsBySubreddit: {
frontend: {
isFetching: true,
didInvalidate: false,
items: []
},
reactjs: {
isFetching: false,
didInvalidate: false,
lastUpdated: 1439478405547,
items: [{
id: 42,
title: 'Confusion about Flux and Relay'
},
{
id: 500,
title: 'Creating a Simple Application'
}]
}
}
}
{ type: 'LIKE_ARTICLE', articleId: 42 }
{ type: 'ADD_TODO', text: 'Read the Redux docs.' }
{ type: 'FETCH_POSTS_REQUEST' }
{ type: 'FETCH_POSTS_FAILURE', error: 'Oops' }
{ type: 'FETCH_POSTS_SUCCESS', response: { ... } }
function reducer(state = initialState, action) {
switch (action.type) {
case 'ADD_TODO':
return Object.assign({}, state, {
todos: [...state.todos, {
text: action.text
}]
});
case 'SET_VISIBILITY_FILTER':
return Object.assign({}, state, {
visibilityFilter: action.filter
});
default:
return state;
}
}