簡體   English   中英

警告:setState(…):只能更新已安裝或正在安裝的組件。 這通常意味着

[英]Warning: setState(…): Can only update a mounted or mounting component. This usually means

我正在Udemy Advanced Webdeveloper訓練營中進行練習。 練習要求拿出一個由32個方框組成的頁面,它們隨機改變顏色(每x秒)。 我的解決方案不完全是這樣。 我同時更改所有32個框的顏色。 它幾乎可以工作。 我最初會隨機得到32個盒子,但以后不會更改顏色。 我的控制台告訴我setState做錯了。 但我不知道是什么。 我認為我的changeColor是一個純函數:

import React, { Component } from 'react';
import './App.css';

class Box extends Component {
    render() {

        var divStyle = {
            backgroundColor: this.props.color
        }

        return(
            <div className="box" style={divStyle}></div>
        );
    }
}

class BoxRow extends Component {
    render() {
        const numOfBoxesInRow = 8;
        const boxes = [];

        for(var i=0; i < numOfBoxesInRow; i++) {
            boxes.push(<Box color={this.props.colors[i]} key={i+1}/>);
        }

        return(
            <div className="boxesWrapper">
            {boxes}
            </div>
            );
    }
}

class BoxTable extends Component {

    constructor(props) {
        super(props);
        this.getRandom = this.getRandom.bind(this);
        this.changeColors = this.changeColors.bind(this);
        this.state = {
            randomColors: this.getRandom(this.props.allColors, 32) // hardcoding
        };

        this.changeColors();
    }

    changeColors() {
        setInterval(
            this.setState({randomColors: this.getRandom(this.props.allColors, 32)}), 5000);
    }

    getRandom(arr, n) {
        var result = new Array(n),
            len = arr.length,
            taken = new Array(len);
        if (n > len)
            throw new RangeError("getRandom: more elements taken than available");
        while (n--) {
            var x = Math.floor(Math.random() * len);
            result[n] = arr[x in taken ? taken[x] : x];
            taken[x] = --len in taken ? taken[len] : len;
        }
        return result;
    }

    render () {
        const numOfRows = 4;
        const rows = [];

        for(let i=0; i < numOfRows; i++) {
          rows.push(
              <BoxRow colors={this.state.randomColors.slice(8*i,8*(1+i))} key={i+1}/>
            )
        }

        return (
          <div className="rowsWrapper">
          {rows}
          </div>
        );
    }
}

BoxTable.defaultProps = {
    allColors: ["AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige",
    "Bisque","Black","BlanchedAlmond","Blue","BlueViolet","Brown","BurlyWood",
    "CadetBlue","Chartreuse","Chocolate","Coral","CornflowerBlue","Cornsilk",
    "Crimson","Cyan","DarkBlue","DarkCyan","DarkGoldenRod","DarkGray","DarkGrey",
    "DarkGreen","DarkKhaki","DarkMagenta","DarkOliveGreen","Darkorange",
    "DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue",
    "DarkSlateGray","DarkSlateGrey","DarkTurquoise","DarkViolet","DeepPink",
    "DeepSkyBlue","DimGray","DimGrey","DodgerBlue","FireBrick","FloralWhite",
    "ForestGreen","Fuchsia","Gainsboro","GhostWhite","Gold","GoldenRod","Gray",
    "Grey","Green","GreenYellow","HoneyDew","HotPink","IndianRed","Indigo",
    "Ivory","Khaki","Lavender","LavenderBlush","LawnGreen","LemonChiffon",
    "LightBlue","LightCoral","LightCyan","LightGoldenRodYellow","LightGray",
    "LightGrey","LightGreen","LightPink","LightSalmon","LightSeaGreen",
    "LightSkyBlue","LightSlateGray","LightSlateGrey","LightSteelBlue",
    "LightYellow","Lime","LimeGreen","Linen","Magenta","Maroon",
    "MediumAquaMarine","MediumBlue","MediumOrchid","MediumPurple",
    "MediumSeaGreen","MediumSlateBlue","MediumSpringGreen","MediumTurquoise",
    "MediumVioletRed","MidnightBlue","MintCream","MistyRose","Moccasin",
    "NavajoWhite","Navy","OldLace","Olive","OliveDrab","Orange","OrangeRed",
    "Orchid","PaleGoldenRod","PaleGreen","PaleTurquoise","PaleVioletRed",
    "PapayaWhip","PeachPuff","Peru","Pink","Plum","PowderBlue","Purple",
    "Red","RosyBrown","RoyalBlue","SaddleBrown","Salmon","SandyBrown",
    "SeaGreen","SeaShell","Sienna","Silver","SkyBlue","SlateBlue","SlateGray",
    "SlateGrey","Snow","SpringGreen","SteelBlue","Tan","Teal","Thistle",
    "Tomato","Turquoise","Violet","Wheat","White","WhiteSmoke","Yellow","YellowGreen"]
}

export default BoxTable

您需要使用lambda函數才能在setInterval中使用setState

setInterval(() => {
        this.setState({randomColors: this.getRandom(this.props.allColors, 
                       32)});
                  }, 5000)

嘗試將您的changeColors函數更改為這樣:

changeColors() {
        setInterval(() => this.setState({randomColors: this.getRandom(this.props.allColors, 32)}), 5000);
    }

setInterval的第一個參數是function,在您的原始代碼中,您已經執行了setState並且沒有傳遞函數本身

在組件創建階段之后,您將需要在componentDidMount()內部更新狀態。

class BoxTable extends Component {

    constructor(props) {
        super(props);
        this.getRandom = this.getRandom.bind(this);
        this.changeColors = this.changeColors.bind(this);
        this.state = {
            randomColors: this.getRandom(this.props.allColors, 32) // hardcoding
        };

        // delete this line 
        //this.changeColors();
    }
    // replace changeColors by componentDidMount, 
    // this function will be called automatically by react
    componentDidMount() {
        setInterval(
            this.setState({randomColors: this.getRandom(this.props.allColors, 32)}), 5000);
    }

    getRandom(arr, n) {
        var result = new Array(n),
            len = arr.length,
            taken = new Array(len);
        if (n > len)
            throw new RangeError("getRandom: more elements taken than available");
        while (n--) {
            var x = Math.floor(Math.random() * len);
            result[n] = arr[x in taken ? taken[x] : x];
            taken[x] = --len in taken ? taken[len] : len;
        }
        return result;
    }

    render () {
        const numOfRows = 4;
        const rows = [];

        for(let i=0; i < numOfRows; i++) {
          rows.push(
              <BoxRow colors={this.state.randomColors.slice(8*i,8*(1+i))} key={i+1}/>
            )
        }

        return (
          <div className="rowsWrapper">
          {rows}
          </div>
        );
    }
}

您正在(從構造函數中)安裝組件之前調用this.setState 嘗試改用componentDidMount生命周期函數中的第一個this.ChangeColors

此外,清除在componentWillUnMount卸載的間隔並不是一個壞主意

編輯:通過改變間隔來叫你做的第一等待防止錯誤之后, 現在 我建議使用生命周期功能來養成良好的習慣。 這只是一個臨時任務,但是在一個完整的項目中,您可能會this.setState在稍后再次破壞該組件,方法是可以在可靠安裝之前調用this.setState

暫無
暫無

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

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