Estado é o conjunto de todos os valores armazenados por uma aplicação como propriedades ou variáveis em qualquer dado instante.
const action = {
type: "CREATE_TASK",
data: {
text: "Comprar ração de cachorro",
dueDate: new Date(Date.now() + 24 * 60 * 60 * 1000),
isDone: false,
},
};
this.props.dispatch(action);
const reducer = (state, action) => {
switch(action.type) {
case "CREATE_TASK":
return {
...state,
tasks: [...state.tasks, action.data],
}
default:
return state;
}
};
const tasksSelector = state => state.tasks;
// a porcentagem só é recalculada
// quando o resultado de tasksSelector mudar
const percentageDoneSelector = createSelector(
tasksSelector,
tasks => {
const doneTasksCount = tasks.filter(x => x.isDone).length;
const tasksCount = tasks.length;
return 100 * doneTasksCount / tasksCount;
}
);
class TaskStore {
@observable tasks = [];
@computed get percentageDone() {
const { tasks } = this;
return tasks.filter(x => x.isDone).length / tasks.length;
}
@action createTask = (task) => {
this.tasks.push(task);
}
}