Skip to content

Instantly share code, notes, and snippets.

@charbelrami
Last active January 7, 2019 17:54
Show Gist options
  • Save charbelrami/e3776fdbfdfb77b1c65a28ee3a01856e to your computer and use it in GitHub Desktop.
Save charbelrami/e3776fdbfdfb77b1c65a28ee3a01856e to your computer and use it in GitHub Desktop.
Logic Hook
import { useState, useContext } from 'react';
import { useDispatch, StoreContext } from 'redux-react-hook';
export default function useTodo() {
const [newTodo, setNewTodo] = useState('');
const store = useContext(StoreContext);
const todos = store.getState();
const dispatch = useDispatch();
function addTodo() {
dispatch({ type: 'ADD_TODO', text: newTodo });
setNewTodo('');
}
return { todos, newTodo, setNewTodo, addTodo };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment