loading...

re: React Context with useReducer and Typescript. VIEW POST

FULL DISCUSSION
 

how the create and delete works?
your code is not complete.

 

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,
    }
  })
}
code of conduct - report abuse