首页 > 解决方案 > 有没有办法根据 id 更改单击的按钮的文本

问题描述

const [toFollow, setToFollow] = useState('follow')

const follow = (user_id) => {
        users && users.map(user => {
                if(user.user_id !== user_id){
                  setToFollow('following')
                }   
            })  
  }

这是数组。问题是当我只想要点击更改的 id 时,整个跟随道具都会发生变化

  <FlatList
      style={{marginBottom: 60}}
      showsVerticalScrollIndicator = {false}
      data={users && users}
      renderItem={({ item }) => (
      <UserFollowCard
        p_image={item.profile_image}
        name={item.name_of_user}
        username={item.username}
        textToFollow={toFollow}
        follow = {() => follow(item.user_id)}
      />
          )}
           keyExtractor={item => item.user_id}
   />

t

this is the flatlist rendering the array with the component

标签: reactjsreact-native

解决方案


我对这个问题不是很清楚。您可以根据条件更改按钮标题。我写了一些代码,这里是链接。您可以使用 TouchableOpacity 代替 Button。


推荐阅读