簡體   English   中英

使用按鈕更改背景顏色 react-native

[英]change background color with a button react-native

我正在嘗試通過單擊按鈕來更改我的視圖背景顏色。 但是什么也沒發生(沒有錯誤)。

export default class App extends React.Component{

    state = {
        color: "#fff"
        }

        render() {
            
            return(
                
                <View style={{backgroundColor: this.state.color, flex: 1, alignItems:'center', justifyContent:'center',}}>
                  
                  <Button  onClick={() => { this.setState({color: "#00ff00" }) }} title="selam"></Button>

                </View>
                      
            );
        }
};

您必須使用 onPress 而不是 onClick

<Button onPress={() => { this.setState({color: "#00ff00" }) }} title="selam"></Button>

暫無
暫無

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

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