I am calling useCallback hook and passing it data returned from an api. The useCallback hook is formatting the returned data and setting it in the state
I expect updated data right after calling the useCallback hook. My question is, will I get updated data in the very next line after calling useCallback hook? Moreover, what can I do to ensure that I do get updated data?
Here is how the code looks like:
const [data, setData] = React.useState([]);
const processPeople = React.useCallback((peopleData) => {
// we will do a lot of formatting here on peopleData and then will do:
setData(peopleData)
}, []}
const loadOptions = React.useCallback((callback) => {
getPeopleData({ // calling API
variables: {
perPage: 20,
page: 1,
},
}).then((response) => {
processPeople(response) // This is a lengthy callback which processes response
console.log(data) // How to make sure I get updated data here?
// callback(data) This is a built-in method of a library which requires updated data
// Please see loadOptions is a library's method
}
}, [data] }
return(
<SingleAsyncSelect
options={peopleDataOptions}
getOptions={loadOptions}
selectOption={handleAdd}
/>
)