简体   繁体   English

如何在React Native中更改按钮的颜色而不重置状态?

[英]How can I change the colour of a button in react native without reseting state?

I'm currently building a screen in react native that has a bunch of horizontal flatLists stacked vertically. 我目前正在用react native构建一个屏幕,该屏幕具有一堆垂直堆叠的水平水平列表。 Think uber eats, Spotify and Netflix. 想想超级吃,Spotify和Netflix。

The horizontal flatLists contain cards. 水平flatList包含卡片。

I have added like and share buttons to these cards. 我在这些卡片上添加了喜欢和分享按钮。 I want my users to be able to save or like the item in the card while scrolling. 我希望我的用户在滚动时能够保存或喜欢卡片中的项目。 The way you can on instagram. 您可以在instagram上的方式。

I click one the buttons to send a request to the backend which works. 我单击一个按钮将请求发送到后端。

The problem I am facing is that I want the buttons to change colour when I press them. 我面临的问题是我希望按钮在按下时能改变颜色。

What I would normally do is place a new variable in state, that gets changed when the request to the backend is done. 我通常要做的是将一个新变量置于状态,当对后端的请求完成时,该变量会更改。

The problem with this approach is that, it causes state to reload. 这种方法的问题在于,它导致状态重新加载。 This cause my component screen to reload. 这导致我的组件屏幕重新加载。 This is not an ideal user experience. 这不是理想的用户体验。 I don't want the whole screen to flash when someone presses a button. 我不希望有人按下按钮时整个屏幕闪烁。

I also don't want my users to lose their place on the screen. 我也不希望我的用户在屏幕上失去位置。

Is there a better approach that allows me to: 1) press the button 2) update the backend 3) Have my button change colour. 有没有更好的方法可以让我:1)按下按钮2)更新后端3)让我的按钮更改颜色。

constructor(props) {
super(props)
this.state = {
  card: [],  
  saved: false,
  shared: false
}

} }

  <FlatList
          data={card}
          keyExtractor={(index, item) => Math.random().toString()}
          horizontal
          renderItem={({ item }) => {
          return (
            <View>
              <TouchableOpacity
                onPress={() => this.description(item)}
              >
                <Card image={{uri: item.img}} containerStyle={{width: width * 0.8, height: height * 0.4}} >
                  <View style={{height: 60, justifyContent: 'center'}}>
                    <Text style={[textStyle, {fontSize: 16}]}>{item.name}</Text>          
                  </View> 
                  <View style={{ flexDirection: 'row', alignItems: 'center', justifyContent:'space-between', width: width * 0.7 }}>
                    <View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>

                    </View>
                    <View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
                      <View>
                        <Icon
                          name='bookmark'
                          type='Feather'
                          onPress={() =>  this.save(item._id)}
                          color={userHasPressedSave.includes(item._id) ? '#FF0000' : null}
                        />
                      </View>
                      <View>
                        <Icon
                          name='ios-heart'
                          type='ionicon'
                          onPress={() => this.shared(item._id)}
                        />
                      </View>
                    </View>
                  </View>
                </Card>
              </TouchableOpacity>
            </View>
          )
          }
        }
        />

This should be obvious ... If you don't want to modify 'global'/parent state then use 'local'/child state - just convert buttons into components. 这应该很明显...如果您不想修改“全局” /父状态,则使用“本地” /子状态-只需将按钮转换为组件即可。

Pass handlers to be able to call backend requests. 传递处理程序以能够调用后端请求。

Use their local state to change color, content (like/unlike label), enable/disable, counter... whatever you need ... just 'think in react' ... tree of components. 使用它们的本地状态来更改颜色,内容(如标签一样/不一样),启用/禁用,计数器...您需要的任何东西...只需“思考一下” ...组件树。

You can also store changes in parent (within handlers) as class/object properties. 您还可以将更改存储在父级(在处理程序内)作为类/对象属性。 Updating them (as opposed to updating the state) won't force rerendering but you will be able to use them when a rerender occurs. 更新它们(而不是更新状态)不会强制重新渲染,但是您可以在重新渲染时使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM