未处理的JS异常:TyperError:未定义不是(评价“_this.onPress.bind”)的对象

问题描述 投票:0回答:1

我试图让一个按钮在本地做出反应使用TouchableOpacity的onPress。我之前的工作,但现在,我加入“喜欢”功能的一些额外的功能,它不工作了。

该onPress应给予与函数的参数。

我得到了我的iOS模拟器错误Unhandled JS Exception: TyperError: undefined is not an object (evaluating '_this.onPress.bind')

我的代码:

export default class App extends Component {
  constructor(props) {
    super(props)
    
    this.state = { liked: [false, false, false] }
    this.onPress = this.onPress.bind(this)
  }

  like = (id) => {
    const liked = this.state.liked
    liked[id] = !liked[id]
    this.setState({
      liked: [!this.state.liked[0], false, false ],
    });
  }



  render() {
    return (
      <Swiper style={SliderStyles.wrapper} showsButtons={false}>
        <View style={SliderStyles.slide1}>
          <View style={CardStyles.card}>
            <View style={CardStyles.card_img} >
              <Image source={require('./recources/ny.jpg')} style={CardStyles.images}/>
            </View>
            <View style={CardStyles.card_details}>
              <Text style={TextStyles.card_title} >New York</Text>
              <View style={CardStyles.card_action}>
                <TouchableOpacity style={CardStyles.card_button} onPress={() => this.like(0)}>
                  
                  <Image source={ this.state.liked[0] === true ? 
                                  require('./recources/icons/heart_closed.png') : 
                                  require('./recources/icons/heart_open.png')
                                } style={CardStyles.card_button_img}/>
                </TouchableOpacity>
              </View>
              <Text style={TextStyles.card_name}>Seppe De Langhe</Text>
            </View>
          </View>
        </View>
      </View>
      </Swiper>
    );
  }
}

Picture of what my iOS simulator looks like

谢谢您的帮助!

javascript reactjs react-native ios-simulator
1个回答
0
投票

看起来你不必绑定一个onPress方法,因为您使用它已经绑定箭头功能的方法被称为like和。

export default class App extends Component {
  state = {
    liked: [false, false, false],
  };

  like = (id) => {
    this.setState(state => {
      state.liked[id] = !state.liked[id];
      return state;
    });
  }

  render() {
    return (
      <Swiper style={SliderStyles.wrapper} showsButtons={false}>
        <View style={SliderStyles.slide1}>
          <View style={CardStyles.card}>
            <View style={CardStyles.card_img} >
              <Image source={require('./recources/ny.jpg')} style={CardStyles.images}/>
            </View>
            <View style={CardStyles.card_details}>
              <Text style={TextStyles.card_title} >New York</Text>
              <View style={CardStyles.card_action}>
                <TouchableOpacity style={CardStyles.card_button} onPress={() => this.like(0)}>
                  
                  <Image source={ this.state.liked[0] === true ? 
                                  require('./recources/icons/heart_closed.png') : 
                                  require('./recources/icons/heart_open.png')
                                } style={CardStyles.card_button_img}/>
                </TouchableOpacity>
              </View>
              <Text style={TextStyles.card_name}>Seppe De Langhe</Text>
            </View>
          </View>
        </View>
      </View>
      </Swiper>
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.