【问题标题】:how to show multiple selected item from list in react native如何在本机反应中显示列表中的多个选定项目
【发布时间】:2021-03-10 19:54:11
【问题描述】:

没有选择列表中的项目,但我想选择所有三个标记的项目,我如何从列表中标记所选项目=?

const List = [
    {
        options: [{ opt: 'a', oid: '1' }, { opt: 'b', oid: '2' }, { opt: 'c', oid: '3' }, { opt: 'd', oid: '4' },
        ],
        marked: [1, 2, 3],
    }
]

const [index, setIndex] = useState(0);
const qstn = List[index];

{qstn.options.map((x, id) =>
                            <Option
                                value={x.opt}
                                selected={qstn.marked === id} // here i am selecting items from marked
                                key={id}
                            />
                        )}

export function Option({ value, selected }) {
    return (
        <TouchableWithoutFeedback>
            <View style={styles.option}>
                <View style={{ flex: 8, backgroundColor:selected? '#b8de6f' : '#fff' }}>
                    <Text style={{ fontSize: 15, fontFamily: 'OpenSans-Regular', }}>{value}</Text>
                </View>
            </View>
        </TouchableWithoutFeedback>
    )
}

【问题讨论】:

    标签: react-native multipleselection


    【解决方案1】:

    selected={qstn.marked === id} 我认为您在这里犯了一个错误,因为 id 是一个数字,而 qstn.marked 是一个数组。如果您想检测该 id 是否在您的数组中,请将此行更改为 qstn.marked.indexOf(id) !== -1

    【讨论】:

      猜你喜欢
      • 2021-06-18
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 2020-04-17
      相关资源
      最近更新 更多