Created
April 17, 2020 11:12
-
-
Save ahmedghazi/05582470f1d0deb34c25594900a4d23f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useReducer } from 'react'; | |
function reducer(state, action) { | |
switch (action.type) { | |
case 'add': | |
return [...state, action.item]; | |
case 'remove': | |
return [ | |
...state.slice(0, action.index), | |
...state.slice(action.index + 1) | |
]; | |
default: | |
throw new Error(); | |
} | |
} | |
function FavoriteMovies() { | |
const [state, dispatch] = useReducer(reducer, [{ name: 'Heat' }]); | |
return ( | |
// Use dispatch({ type: 'add', item: movie }) | |
// and dispatch({ type: 'remove', index })... | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment