简体   繁体   English

如何使用React Native修改状态Text?

[英]How can I modify the state Text with React Native?

So basically I am making an app where you swipe right for yes and swipe left for no. 因此,基本上,我正在制作一个应用,其中您向右滑动表示是,向左滑动表示否。 The problem I have now is that I want the right text to appear on the card that changes based on the direction the user is swiping. 我现在遇到的问题是,我希望正确的文本显示在卡上,该卡根据用户的滑动方向而变化。

The PanResponder handles the text being changed by modifing the state.changeText and changes the text based on the direction. PanResponder通过修改state.changeText处理要更改的文本,并根据方向更改文本。 The code partly works at the moment but the problem I have now is that the previous state value for the changeText is what shows during the swiping process as opposed to the current one. 该代码目前部分起作用,但是我现在遇到的问题是,在滑动过程中显示的changeText的先前状态值与当前值不同。

The code is shown below. 代码如下所示。

import React from "react"
import {
  View, Text, Animated, ScrollView, StyleSheet, Button,PanResponder,Dimensions
} from "react-native"
import Icon from "react-native-vector-icons/Ionicons" 
import {Card, Badge} from "react-native-elements"

const screenWidth =Dimensions.get("window").width
const swipeThreshold = screenWidth * .25


export default class SwipeCard extends React.Component{
  constructor(props){
    super(props);
    //this initiates the dragging animation
    const position = new Animated.ValueXY();
    const panResponder = PanResponder.create({
      // if true anytime the user starts to click down on the screen, the panresponder handles the action

        onStartShouldSetPanResponder : () => true,
          //everytime the user drags on the screen
        onPanResponderMove : (event, gesture) => {
          //get the distance they dragged the item by and update it in the animation
          position.setValue({x: gesture.dx, y: gesture.dy})
          this.setState({
            style:{
              opacity:1,
          }})

        },
        //everytime the user lets go
        onPanResponderRelease: (event, gesture) => {
          this.setState({
            style:{
              opacity:0,
          }})
          if(gesture.dx > swipeThreshold){
            console.log("swipe right!");
            this.setState({change: "swipe right!"})
            position.setValue({x:0, y:0});

            this.nextItem();
          }
          else if (gesture.dx < -swipeThreshold){
            console.log("swipe left!");
            this.changeText("swipe left")
            this.setState({change: "swipe left!"})

            position.setValue({x:0, y:0});

            this.nextItem();
          }
          else{
            position.setValue({x:0, y:0});
            this.setState({
              style:{
                opacity:0,
            }})
          }
        }
    })
    this.state={
      index:1,
      panResponder,
      position,
      style:{
        opacity:0,
      },
      changeText:"",
    }
    this.nextItem=this.nextItem.bind(this);
  }


  getCardStlye(){
    const {position} = this.state;
    const rotate = position.x.interpolate({
        inputRange: [-screenWidth * 2, 0, screenWidth*2],
        outputRange: ["-120deg", "0deg", "120deg"]
    });
    return {
      ...position.getLayout(),
      transform: [{rotate}]

    }
  }



  nextItem() {
        if(this.state.index !== this.props.data.length){
           this.setState({ index: this.state.index + 1 })
       }
       else {
          this.setState({ index:0});
        }

  }
  render(){
    var cloth = this.props.data.find((cloth, i)=>{
      return i+1 === this.state.index;
    })

    return(
        <ScrollView>
           {this.state.index ?(
        <Animated.View
           key ={cloth.deal.id}
           style={this.getCardStlye()}
           {...this.state.panResponder.panHandlers}>
           <Card
             title={cloth.deal.merchant.name}
             image ={{uri:cloth.deal.image_url}}
             >
             <Text
                style ={this.state.style}>
                {this.state.changeText}
                </Text>
             <Text>${cloth.deal.price}</Text>
             <Button
               title="Details"
               backgroundColor="blue"
             ></Button>
             </Card>
         </Animated.View>
           )
         :
         <Text>Null</Text>
           }
       </ScrollView>
    )
 }


  }

Apparently you have a mistake, you don't have a changetext method in your class yet you're using it in swiping left case, try to remove it and check if it works 显然您犯了一个错误,您的班级中没有changetext方法,但是您正在用它擦拭左写字母,尝试将其删除并检查是否有效

      if(gesture.dx > swipeThreshold){
        console.log("swipe right!");
        this.setState({change: "swipe right!"})
        position.setValue({x:0, y:0});

        this.nextItem();
      }
      else if (gesture.dx < -swipeThreshold){
        console.log("swipe left!");
        this.changeText("swipe left") // <<< HERE
        this.setState({change: "swipe left!"})

        position.setValue({x:0, y:0});

        this.nextItem();
      }
      else{

Actually I realized my mistake. 其实我意识到自己的错误。 Adding the setState to onPanResponderMove did the trick 将setState添加到onPanResponderMove可以达到目的

const panResponder = PanResponder.create({
  // if true anytime the user starts to click down on the screen, the panresponder handles the action

    onStartShouldSetPanResponder : () => true,
      //everytime the user drags on the screen
    onPanResponderMove : (event, gesture) => {
      //get the distance they dragged the item by and update it in the animation
      position.setValue({x: gesture.dx, y: gesture.dy})
      if(gesture.dx > swipeThreshold){
        console.log("swipe right!");
        this.setState({changeText: "swipe right!"})
      }
      else if (gesture.dx < -swipeThreshold){
        console.log("swipe left!");
        this.setState({changeText: "swipe left!"})
      }
      else{
        this.setState({
          style:{
            opacity:0,
        }})
      }
      this.setState({
        style:{
          opacity:1,
      }
    })

    },
    //everytime the user lets go
    onPanResponderRelease: (event, gesture) => {
      this.setState({
        style:{
          opacity:0,
      }})
      if(gesture.dx > swipeThreshold){
        console.log("swipe right!");
        this.setState({changeText: ""})
        position.setValue({x:0, y:0});
        this.addItems()
        this.nextItem();
      }
      else if (gesture.dx < -swipeThreshold){
        console.log("swipe left!");
        this.setState({changeText: ""})
        position.setValue({x:0, y:0});
        this.nextItem();
      }
      else{
        position.setValue({x:0, y:0});
        this.setState({
          style:{
            opacity:0,
        }})
      }
    }
})
this.state={
  index:1,
  panResponder,
  position,
  style:{
    opacity:0,
  },
  changeText:"",
  items:[],
  itemName:"",
}

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

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