Skip to content

Instantly share code, notes, and snippets.

@MatthiasKainer
Last active July 5, 2020 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthiasKainer/eaaedeebdf4da15d7983a294eea9eb0f to your computer and use it in GitHub Desktop.
Save MatthiasKainer/eaaedeebdf4da15d7983a294eea9eb0f to your computer and use it in GitHub Desktop.
const TodoReducer: Reducer<TodoState> = (state) => ({
add: (payload: string) => ({ ...state, todos: [...state.todos, payload as string] }),
remove: (payload: string) => ({
...state,
todos: [...state.todos.filter((todo) => todo !== payload)],
})
})
pureLit("reducer-todo", (element) => {
const { publish, getState } = useReducer(element, TodoReducer, { todos: [] } as TodoState);
return html`
<h2>Your todos</h2>
<todo-add
@add="${(e: CustomEvent) =>
publish("add", e.detail)
}"
></todo-add>
<todo-list
.items="${getState().todos}"
@remove="${(e: CustomEvent) =>
publish("remove", e.detail)
}"
></todo-list>
`;
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment