第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

自定義單選按鈕 React Native

自定義單選按鈕 React Native

哈士奇WWW 2021-11-04 16:43:48
嘿,所以我是本機(jī)和 javascript 的新手,目前我正在學(xué)習(xí)制作一個(gè)帶有圖像的自定義單選按鈕,它看起來(lái)像這樣我在此頁(yè)面中的自定義單選按鈕用戶將從列表中選擇一個(gè)按鈕,我想要在頁(yè)面第一次渲染時(shí)顯示一個(gè)按下的按鈕,并且用戶只能選擇一個(gè)按鈕。誰(shuí)能告訴我如何解決這個(gè)問(wèn)題?提前致謝這是我的代碼單選按鈕.jsexport default class RadioButton extends Component {    constructor(props) {        super(props);        this.state = {            selected: this.props.currentSelection === this.props.value,        }    }    button() {        var imgSource = this.state.selected? this.props.normalImg : this.props.selectedImg;        return (          <Image            source={ imgSource }          />        );      }      render() {        let activeButton = this.props.activeStyle ? this.props.activeStyle : styles.activeButton;        return (            <View style={[styles.container, this.props.containerStyle, this.state.selected || this.props.normalImg ? activeButton : inactiveButton]}>                <TouchableOpacity onPress={() => {                    this.setState({ selected: !this.state.selected });                                    }}>                    {                       this.button()                    }                </TouchableOpacity>            </View>        );    }}活動(dòng)日志.jsclass ActivityLog extends Component {    constructor(props){        super(props);     }
查看完整描述

1 回答

?
富國(guó)滬深

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊

將activeStatus提取到ActivityLog中來(lái)跟蹤哪個(gè)按鈕被選中,現(xiàn)在你為每個(gè)按鈕維護(hù)一個(gè)狀態(tài)作為本地狀態(tài)。所以很難知道其他組件知道按鈕的活動(dòng)狀態(tài)。


這是一個(gè)粗略的想法的通用實(shí)現(xiàn)。


const Child=(props)=>{

    <TouchableOpacity onPress={props.handlePress}>

        <Text style={[baseStyle,active && activeStyle]}>{props.title}</Text>

    </TouchableOpacity>

}

class Parent extends React.Component{

 state={

  selectedChild:''

 }

 changeSelection=(sometitle)=>{

  this.setState({

      selectedChild:sometitle

  })

 }

 render(){

     return(

         <View>

             <Child handlePress={()=>this.changeSelection('1')} active={this.state.selectedChild==='1'}/>

             <Child handlePress={()=>this.changeSelection('2')} active={this.state.selectedChild==='2'}/>

             <Child handlePress={()=>this.changeSelection('3')} active={this.state.selectedChild==='3'}/>

             <Child handlePress={()=>this.changeSelection('4')} active={this.state.selectedChild==='4'}/>

         </View>

     )

 }

}


查看完整回答
反對(duì) 回復(fù) 2021-11-04
  • 1 回答
  • 0 關(guān)注
  • 267 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)