【问题标题】:react native : Unable to uncheck checkbox反应原生:无法取消选中复选框
【发布时间】:2021-06-13 09:05:55
【问题描述】:

在我的示例中,我提供了一个复选框列表,但是 我无法取消选中复选框。 现在我只能检查它们但不能取消选中。 我应该如何修复 我的代码 才能正常工作? ?

 const [checkList, setCheckList] = useState<any[]>(actionList.map(e => ({ key: e.key, checked: false })));
  const isChecked = (key) => {
    checkList.filter(e => e.key == key)[0].checked
  }


  const renderActionItem = ({ item, index }) => {
    return (
      <TouchableOpacity
        key={index}
        activeOpacity={.7}
        style={styles.touchableOpacity}
        onPress={() => { console.log(item) }}
      >
        <CheckBox
          style={styles.checkBox}
          tintColors={{ true: 'white', false: 'white' }}
          value={isChecked(item.key)}
          onValueChange={() => {
            const newCheckList = checkList.slice()
            newCheckList.forEach(e => {
              if (e.key == item.key) {
                e.checked = !e.checked
              }
            })
            setCheckList(newCheckList)
          }}
        />
        <Text style={styles.labelText}>{`${item.sequenceID} . ${item.label}`}</Text>
      </TouchableOpacity>
    )
  }

【问题讨论】:

  • 你要加一个return return checkList.filter(e =&gt; e.key == key)[0].checked;
  • 告诉我你的想法是如何融入我的代码的......
  • const isChecked = (key) =&gt; { return checkList.filter(e =&gt; e.key == key)[0].checked }

标签: javascript reactjs typescript react-native


【解决方案1】:
import React, { Component } from 'react';
import { CheckBox } from 'native-base';
import { View, Text, StyleSheet} from 'react-native';

class Row extends Component {
  constructor(props) {
    super(props);
    this.state = { checked: false };
  }

  render() {
    return (
      <View style={styles.container}>
        <CheckBox
          onPress={() => this.setState({
            checked: !this.state.checked
          })}
          checked={this.state.checked}
        />
        <Text style={styles.text}>
          { props.data }
        </Text>
      </View>
    );
  }
}

export default Row;

【讨论】:

  • 我问如何更改我的代码而不是新的代码想法。
  • 缺乏解释。
  • 它不适合我。只需将我的代码放在上面并进行更改即可。我不需要你在这里写的所有例子。我只想修复我的代码,这就是我的朋友。
【解决方案2】:

只需像这样使用状态来管理您的项目:

const renderActionItem = ({ item, index }) => {
const [checkbox, setCheckbox] = useState(false);
    return (
      <TouchableOpacity
        key={index}
        activeOpacity={.7}
        style={styles.touchableOpacity}
        onPress={() => { console.log(item) }}
      >
        <CheckBox
          style={styles.checkBox}
          tintColors={{ true: 'white', false: 'white' }}
          value={isChecked(item.key)}
          onValueChange={() => {
            useState(!checkbox)
          }}
        />
        <Text style={styles.labelText}>{`${item.sequenceID} . ${item.label}`}</Text>
      </TouchableOpacity>
    )
  }

然后根据状态变化设置自定义检查图标。或者,您可以按照以下示例进行操作:请注意,在这种情况下,我使用 SelectMultiple 而不是复选框

import React, { Component } from 'react'
import { View } from 'react-native'
import SelectMultiple from 'react-native-select-multiple'

const fruits = ['Apples', 'Oranges', 'Pears']
// --- OR ---
// const fruits = [
//   { label: 'Apples', value: 'appls' },
//   { label: 'Oranges', value: 'orngs' },
//   { label: 'Pears', value: 'pears' }
// ]

class App extends Component {
  state = { selectedFruits: [] }

  onSelectionsChange = (selectedFruits) => {
    // selectedFruits is array of { label, value }
    this.setState({ selectedFruits })
  }

  render () {
    return (
      <View>
        <SelectMultiple
          items={fruits}
          selectedItems={this.state.selectedFruits}
          onSelectionsChange={this.onSelectionsChange} />
      </View>
    )
  }
}
export default App

【讨论】:

  • 顺便说一句,根据反应原生文档,您在代码中使用的复选框现在已弃用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
相关资源
最近更新 更多