简体   繁体   English

如何使用 React Native 制作类似于 Instagram 的点赞功能(如按钮)

[英]How to make a like function similar to Instagram (like button) with react native

Hello I am trying to make a Instagram like button with react-native by changing the image on press, but when I press the image no changes您好,我正在尝试通过在按下时更改图像来制作带有 react-native 的类似 Instagram 的按钮,但是当我按下图像时没有任何更改

Everything is up to date.一切都是最新的。 And the code has no errors.并且代码没有错误。

Here is my code:这是我的代码:

import React, { Component } from 'react';
import { View, Text, StyleSheet, Image,navigation,TouchableOpacity } from 'react-native';
import { Container, Content, Badge, Left, Icon, Header} from 'native-base';
import { DrawerActions } from 'react-navigation';

class HomePage extends Component {
    constructor(props){
        super(props)
        this.state = {
        likedQ: false,   
         uri: require('./images/mianIcons/like.png') 
        }
    }

    _ifLiked = () => {
        likedQ = true;
        uri: require('./images/mianIcons/like3.png') 
    }

    render(){
        return(
               <View>
                    <Header>
                        <Left>
                          <Icon name="ios-menu" onPress={()=> this.props.navigation.openDrawer()}
                          />
                        </Left>
                   </Header>   

                  <TouchableOpacity onPress={this._ifLiked()}>  
                    <Image 
                    style={{width: 32 , height: 32 ,}}
                    source={require(uri)}
                   />
                  </TouchableOpacity>          
           </View>           

        )
    }
}

export default HomePage;

You should set state to rerender the component您应该设置状态以重新渲染组件

_ifLiked = () => { 
  this.setState({
    likedQ: true, 
    uri: require('./images/mianIcons/like3.png')
  })
} 

change your onPress function like this.像这样改变你的 onPress 功能。 It is basic thing that you should set state for react components to update您应该为反应组件设置状态以进行更新,这是基本的事情

There are some more corrections.还有一些更正。 Following is the corrected code以下是更正后的代码

class HomePage extends Component{ 
  constructor(props){ 
    super(props) 
    this.state = { 
      likedQ: false, 
      uri: './images/mianIcons/like.png' 
    } 
  } 

  _ifLiked = () => { 
  this.setState({
    likedQ: true, 
    uri: require('./images/mianIcons/like3.png')
  }
}  

  render(){ 
   return( 
    <View> 
      <Header> 
        <Left> 
          <Icon name="ios-menu" onPress={()=> this.props.navigation.openDrawer()} /> 
       </Left> 
     </Header> 
     <TouchableOpacity onPress={() => this._ifLiked}> 
       <Image style={{width: 32 , height: 32}} source={require(this.state.uri)} /> 
     </TouchableOpacity> 
   </View> 
 )} 
}

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

相关问题 如何在 React Native 中制作类似功能? - How to make like function in React Native? 在React Native中显示像Instagram一样的图像部分吗? - Show the part of the image like Instagram in React Native? Instagram 喜欢使用 React Native 的过滤器 - Instagram like filters using React Native 我想知道如何进行自动刷新而不是在 Expo/React Native 中按下按钮 - I would like to know how to make automatic refresh instead of pressing a button in Expo/React Native 如何像切换按钮一样对 function 组件做出反应 - How to react function component in like a toggle button react native 如何从另一个 class 的 function (如组件)进行回调 - react native How to make a callback from a function of an other class like a component 如何像在 Instagram 上一样在本机天才聊天的开头渲染自定义组件? - How to render custom component on the beginning of the react native gifted chat like on instagram? 如何使用 React Native 创建像 instagram Reel 这样的快速视频加载功能? - How to create fast video loading feature like instagram Reel with React Native? 我将如何像这样在 react Native 中进行自定义开关和切换 - How would I make a Custom Switch and Toggle Like this In react Native 如何在 React Native 中制作像 UI 这样的表格来渲染其中的元素? - How to make a table like UI in react native to render elements in it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM