Skip to content

Instantly share code, notes, and snippets.

@ahmedghazi
Created April 17, 2020 11:12
Show Gist options
  • Save ahmedghazi/05582470f1d0deb34c25594900a4d23f to your computer and use it in GitHub Desktop.
Save ahmedghazi/05582470f1d0deb34c25594900a4d23f to your computer and use it in GitHub Desktop.
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