Reputation: 47
I am new comer in react-native. How do I clear this useState
array?
const [tokenArray, setTokenArray]=useState([]);
// Need to Clear Array this line.
setTokenArray(tokenArray,[]);
var arraySize = responseJson.status.split(',').length;
for(var x=1;x<arraySize;x++){
console.log("X--->"+x)
setTokenArray(tokenArray => [...tokenArray, responseJson.status.split(',')[x]])
}
Upvotes: 3
Views: 11122
Reputation: 3965
If you want to clear out the array you can just set it to an empty array when calling setTokenArray
.
setTokenArray([]);
Upvotes: 1
Reputation: 26256
You have a syntax error that you probably got by retyping the other setTokenArray
call incorrectly:
setTokenArray(tokenArray,[]);
should be
setTokenArray([]);
// or
setTokenArray(tokenArray => []);
When you pass a function to setTokenArray
, the first argument will be the old value of that variable. This is useful for when you need to mutate it like you do inside the for loop.
Upvotes: 6