I have this component in my react js application:
import React, { useState } from "react";
import Select, { components, DropdownIndicatorProps } from "react-select";
import { ColourOption, colourOptions } from "./docs/data";
const Component = () => {
const [state, setState] = useState();
console.log(state);
const DropdownIndicator = (props) => {
const { menuIsOpen } = props.selectProps;
setState(menuIsOpen);
return (
<components.DropdownIndicator {...props}>12</components.DropdownIndicator>
);
};
return (
<Select
closeMenuOnSelect={false}
components={{ DropdownIndicator }}
defaultValue={[colourOptions[4], colourOptions[5]]}
isMulti
options={colourOptions}
/>
);
};
export default Component;
In the DropDownIndicator component i set the state:
const {
menuIsOpen
} = props.selectProps;
setState(menuIsOpen);
Setting the state in that place i get the next warning: Warning: Cannot update a component (Component) while rendering a different component (DropdownIndicator). To locate the bad setState() call inside DropdownIndicator .
Question: How can i fix this warning and to make it disappear?
demo: https://codesandbox.io/s/codesandboxer-example-forked-97sx0?file=/example.tsx:0-724