I have the next component in my react application:
import React, { useEffect, useState } from "react";
import AsyncSelect from "react-select/async";
import { ColourOption, colourOptions } from "./docs/data";
const App = () => {
const [state, setState] = useState([]);
useEffect(() => {
setState(colourOptions);
}, []);
return (
<AsyncSelect
cacheOptions
defaultOptions
loadOptions={async () => new Promise((r) => r(state))}
/>
);
};
export default App;
There i set colourOptions in useEffect and after that i read the state in loadOptions. Now the options don't appear when i open the select, but when i change loadOptions={async () => new Promise((r) => r(state))} to loadOptions={async () => new Promise((r) => r(colourOptions))} the dropdown items appear.
How to make my code workable in the first situation with state, using useEffect?
demo: https://codesandbox.io/s/codesandboxer-example-forked-b3857?file=/example.tsx:355-409