簡體   English   中英

如何在Flatlist中使用單選按鈕-React Native

[英]How to use Radio button in Flatlist - React native

我想使用平面列表使用單選按鈕創建列表,但是問題是我能夠單擊單選按鈕,它不會在平面列表內部未選中。

這是我的密碼

 <FlatList
                    data={this.state.addressData}
                    horizontal={false}
                    extraData={this.state}
                    showsHorizontalScrollIndicator={false}
                    renderItem={({ item, index }) => (
                        <Card containerStyle={{ backgroundColor: GlobalColors.white, marginLeft: 0, marginRight: 0, marginTop: 0, marginBottom: 10, elevation: 2, padding: 0, borderColor: GlobalColors.white }}>

                            <View style={{ backgroundColor: GlobalColors.white, padding: 15 }}>


                                <View style={SelectAddressStyle.horizontalChangeAddress}>
                                   <Radio style={{marginRight : 10}} selected = {true}></Radio>
                                    <Text style={SelectAddressStyle.txtAddressUserName}>{""}</Text>
                                    <Text style={SelectAddressStyle.addressType}>{"HOME"}</Text>
                                </View>
                                <Text style={SelectAddressStyle.txtAddress}>
                                    {""}
                                </Text>



                            </View>
                        </Card>
                    )} />

你有:

<Radio style={{marginRight : 10}} selected = {true}></Radio>

因此,它不會變得不受限制。 也許嘗試傳遞selected屬性。

idk您正在使用哪個軟件包進行廣播(React本機本身沒有廣播),但是問題在於,根據代碼的這一部分<Radio style={{marginRight : 10}} selected = {true}></Radio> ,您總是將true傳遞給selected prop,因此在任何情況下它都不會改變,直到為它聲明一個switch函數並將其當前狀態存儲在組件狀態中。 例:

class Example extends component{
    state = {
    radioStatus: false
  }

  toggleRadio = () => {
    const {radioStatus} = this.state;
    this.setState({ radioStatus: !radioStatus})
  };

  render(){
    const {radioStatus} = this.state;
    return(
        <View>
          <Radio selected={radioStatus} onChange={this.toggleRadio} />
        </View>
    )
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM