I a functional component in React, and when I use useState hook to set a value in the return, I am getting an error about Maximum update depth exceeded.
Here is the code:
const TaskDetail = (props) => {
const [selectedOperatorForTime, setSelectedOperatorForTime] = useState({})
return (
<div >
<Query query={GET_API_VAL}>
{({ loading, error, data }) => {
let operatorList = {key1: data.value1, key2: data.value2}
setSelectedOperatorForTime(operatorList);
{//rest of the code}
I get the following error
Unhandled Rejection (Error): Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
I tried putting the setState in a separate function instead of having it directly in the return, but I am still seeing the same error. Any guidance? I am using Apollo client (which is what the Query component is for. Unsure how I am causing this to be called repeatedly.
I tried replacing the operatorList with a string, and it works fine. So it seems to be an issue with an object. Anyone experienced this?