Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put the `response.data` value into the `setState[]` array

I want to put swr data into setState[] I have code like this.

const { data } = useSWR('/api/data', fetcher);
const [contents, setcontents] = useState<Array<string>>([]);

How can I write code to put data in setcontents[] ?

like image 430
minsu Avatar asked Jul 20 '26 20:07

minsu


1 Answers

You don’t need to do this. The data is already part of the component’s state, and SWR is managing it for you. You don’t need to transfer the data into another state variable.

Use data anywhere you were expecting to use contents, and it will work as you expected. You can choose a different name for the variable if you like:

const { data: contents } = useSWR(...)
like image 95
Stephen Jennings Avatar answered Jul 23 '26 09:07

Stephen Jennings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!