50}) => {
51 const categories = useSelector(selectCategories);
52 const products = useSelector(selectProducts);
53 const product = useSelector((state: RootState) => { 54 if (DELETE) {
55 return selectProductById(state, DELETE);
56 }
80 <input
81 {...register("name", {
82 required: "Name is required",
83 validate: (data) => { 84 if (data === "") {
85 return "Name is required";
86 } else if (
81);
82export const addProductData = createAsyncThunk(
83 "categories/addProductData",
84 async (data: Partial<Product>) => { 85 try {
86 const product = createProduct(data as Product);
87 await axios.post(`${PRODUCTS_URL}` + ".json", product);
52);
53export const editProductData = createAsyncThunk(
54 "categories/editProduct",
55 async (data: { id: string; newProduct: Partial<Product> }) => { 56 try {
57 const product = await axios.get(`${PRODUCTS_URL}/${data.id}` + ".json");
58 product.data = { ...data.newProduct };
39
40export const deleteProduct = createAsyncThunk(
41 "categories/deleteProduct",
42 async (data: { body: string }) => { 43 try {
44 const response = await axios.delete(
45 `${PRODUCTS_URL}/${data.body}` + ".json"
Any code paths that do not have explicit returns will return undefined
.
It is recommended to replace any implicit dead-ends that return undefined
with a return null
statement.
As a convention, undefined
signals that an unexpected value has been produced as the
result of a logical error in the program.
A null
return, on the other hand, signals that the input to a function was incorrect,
or a value was 'not found'.
Having this distinction in code helps you figure out if something was caused because of a logical error, or malformed input to a function call.
function getUser(name) {
if (userExists(name)) {
return userDb.get(name);
}
// whoops! Implicit undefined return
}
function readFile(fileName) {
if (fs.existsSync(fileName)) {
return fs.readFileSync(fileName, 'utf-8');
}
return; // Implicit undefined return.
}
function getUser(name) {
if (userExists(name)) {
return userDb.get(name);
}
// `null` indicates there is no such user.
return null;
}
function readFile(fileName) {
if (fs.existsSync(fileName)) {
return fs.readFileSync(fileName, 'utf-8');
}
// `null` indicates there is no such file.
return null;
}