-1
New in RN and I’m trying to refactor from the Component class to Function Component to use Hooks, but at the time of setting the function item. map(), here "setSentences(item);", not reacting, someone knows what I’m doing wrong?
this.state = {
sentences: [],
};
{this.state.sentences.map((item) => {
return(
<TouchableOpacity
onPress={() => {
item.selected = false;
this.setState(item);
}}>
</TouchableOpacity>
)}}
Refactored:
const [sentences, setSentences] = useState([]);
{sentences.map((item) => {
return (
<TouchableOpacity
onPress={() => {
item.selected = false;
setSentences(item);
}}>
</TouchableOpacity>
If
sentences
is an array of items, because you are passing only one item to thesetSentences
?– Andre
because every time I click the button, this item just want it updated, the way I did it like this: this.setState(item); it was working, but maybe there’s something wrong here that I don’t know what it is: setSentences(item);.
– wagner tanaka