亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我不斷收到一條錯誤消息,說 this.replaceState 不是函數

我不斷收到一條錯誤消息,說 this.replaceState 不是函數

繁星coding 2022-01-13 15:13:47
現在我有一個學校項目要在我的手機上構建一個應用程序。我們正在使用 React Native 和 Java。我想在應用程序中制作一個按鈕,按下時會變為隨機顏色。然而,這不起作用。這是我的一些代碼片段:    export default class HomeScreen extends React.Component {  constructor(props) {  super(props);  this.state = {color: "#000000"}}  render() {    let color = this.state.color;  return (...<View>          <Button          onPress = {changecolor}          title = "This button changes color when pressed"          color = {color}/>        </View>...numbers = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];this.random = this.numbers[Math.floor(Math.random() * this.numbers.length)];function changecolor() {  this.replaceState({color: String("#"+random+random+random+random+random+random)});} 在此之前我也遇到了一個問題,該按鈕只會變成灰色陰影而不是任何顏色。請問有什么幫助嗎?編輯:我應該提到我之前使用過 this.setState 并且結果相同
查看完整描述

2 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

export default class HomeScreen extends React.Component {


    constructor(props) {

        super(props);

        this.state = {color: "#000000"}

    }


    render() {


    let color = this.state.color;

    return (

    <View>

        <Button

        onPress = {this.changecolor}

        title = "This button changes color when pressed"

        color = {color}/>

    </View>



    numbers = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];


    this.random = this.numbers[Math.floor(Math.random() * this.numbers.length)];


    changecolor() {

        this.replaceState({color: String("#"+random+random+random+random+random+random)});

    }


查看完整回答
反對 回復 2022-01-13
?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

changecolor甚至是班級成員HomeScreen?看起來不像,考慮到它是用聲明的function并且沒有解析錯誤對你大喊大叫。讓它成為一個方法,更好的是,一個箭頭函數:


changecolor = () => {

  this.replaceState({color: String("#"+random+random+random+random+random+random)});


查看完整回答
反對 回復 2022-01-13
  • 2 回答
  • 0 關注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號