Hi, When I’m trying to render the following component I’m getting the error showing too many re-renders. Here is the component
import React, {useState, useContext, useReducer} from "react";
const mealItems = [
{
name:"banana",
id:Math.random()*5,
origin:"india",
price:45
},
{
name:"apple",
origin:"australia",
id:Math.random()*5,
price:92
},
{
name:"orange",
origin:"usa",
id:Math.random()*5,
price:134
}
]
const cartItems = [];
function Input (){
let initialState = 1;
const reducer = function (state, action){
if(action.type === "substract"){
if(state<0){
return;
}
else{
return (state)=>state-=1;
}
}
else if(action.type=== "add"){
return (state)=>state+=1;
}
}
const[state, dispatchFunction] = useReducer(reducer, initialState);
return (
<>
<button onClick = {dispatchFunction({type:"substract"})}>substract</button>
<input type = "number" value = {state}/>
<button onClick = {dispatchFunction({type:"add"})}>addition</button>
</>
)
}
Please help