React Native refactorando class for Function Component

Asked

Viewed 35 times

-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 the setSentences?

  • 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);.

1 answer

0

 {sentences.map((item, index) => {
    return (
      <TouchableOpacity
        onPress={() => {
          item.selected = false;
          sentences[index] = item
          setSentences(sentences);
        }}>
      </TouchableOpacity>
)})}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.