首页 文章

React native custom复选框

提问于
浏览
0

我想在本地创建一个自定义复选框,我几乎在那里,但我无法解决的一个问题是项目的删除/取消选中

这是我的渲染

// Data is a service a created from which i get the data

    {
      Data.specialities.map(category => (
        <TouchableOpacity key={ category.value } onPress={ () => this.check(category) } style={ styles.list_container }>
          <View style={ styles.icon_container }>
            {
              this.state[category.value]
              ? <Icon name="md-checkmark" style={ styles.icon } />
              : null
            }
          </View>
          <Text style={ styles.category }>{ category.value }</Text>
        </TouchableOpacity>
      ))
    }

这是功能

check = async (e) => {

let selected = this.state.categories.map(category => {
  if (category.value == e.value) {
    let remove = this.state.categories.filter(cat => cat.value != e.value)
    this.setState({ categories: remove })
    return;
  } else {
    this.setState(prev => ({ categories: [ ...prev.categories, e ], [e.value]: !prev[e.value] }) )
  }
})
}

似乎 else 声明始终是真实的条件!

1 回答

  • 0

    这里有几件事......

    你在哪里处理 check 函数中传递的 category 参数?您只接受电子参数或事件参数 . 你是否像 category 那样处理电子参数?或者像 event handler 那样?

    如果您使用 e 作为事件参数,那么您想使用 e.target.value 而不是 e.value . 此方法返回给定 <input /> 的值 . 如果你想使用 e 作为你的 category 参数,你必须实际包含一个 category 参数并使用它 .

    check = async (e, categoryParam) {
      //then you can use whatever props the obj category has by doing category.value, category.name, etc. 
    }
    

    您不能调用param类别,因为您已经通过map函数使用了该变量名称,因此其中一个变量名称必须不同 .

    category.valuee.value 的比较并不完全正确 . 您希望使用三等于比较类型和值以返回最佳结果 . 与您的 != 相同,应该 !== 来比较类型和值 . 您应该在控制台中收到此警告 .

相关问题