DEV Community

Discussion on: React Context with useReducer and Typescript.

Collapse
 
elisealcala profile image
Elizabeth Alcalá

Hi, I update the code here. codesandbox.io/s/context-reducer-t..., check the List component. Basically, I create a state to handle the form and then just list the products.

import { AppContext } from "./context";
import { Types } from "./reducers";

const { state , dispatch } = React.useContext(AppContext);

const createProduct = () => {
  dispatch({
    type: Types.Create,
    payload: {
      id: Math.round(Math.random() * 10000),
      name: form.name,
      price: form.price
    }
  });
};

const deleteProduct = (id: number) => {
  dispatch({
    type: Types.Delete,
    payload: {
      id,
    }
  })
}