Warm tip: This article is reproduced from serverfault.com, please click

how to show multiple selected item from list in react native

发布于 2020-11-28 07:25:04

no item in the List is selected but I want to select all three marked items, how I can mark the selected item from the list =?

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>
    )
}
Questioner
Shivam
Viewed
0
Leri Gogsadze 2020-11-28 15:54:23

selected={qstn.marked === id} I think you are making a mistake here because id is a number and qstn.marked an array. If you want to detect whether that id is in your array change this line to qstn.marked.indexOf(id) !== -1.