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

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

盡管更新了 redux 狀態(tài),但組件不會(huì)重新渲染

盡管更新了 redux 狀態(tài),但組件不會(huì)重新渲染

有只小跳蛙 2021-06-17 09:09:08
所以我有 4 個(gè)不同顏色的 4 個(gè)按鈕和 10 個(gè)方塊,它們應(yīng)該具有最后 10 次點(diǎn)擊按鈕的顏色。我映射到 redux 狀態(tài)以顯示正方形。我還將 squareColors 存儲(chǔ)在localStorage.當(dāng)我點(diǎn)擊一個(gè)按鈕時(shí),狀態(tài)會(huì)更新,所以我應(yīng)該立即看到方塊顏色的變化。但是,它僅在我刷新頁面時(shí)發(fā)生。App.js單擊按鈕時(shí),我的組件不會(huì)重新渲染。為什么 ?應(yīng)用程序.js:import React from 'react';import { connect } from 'react-redux';import { setColors } from './redux/actions';import './App.css';import CornerButton from './components/CornerButton';import Square from './components/Square';const styles = // some stylesclass App extends React.Component {  componentDidMount() {    if (localStorage.getItem('lastTenColors')) {      let squareColors = JSON.parse(localStorage.getItem('lastTenColors'));      this.props.setColors(squareColors);    } else {      let squareColors = localStorage.setItem('lastTenColors',  JSON.stringify([...Array(10)]));      this.props.setColors(squareColors);    }  }  render() {    return (          <div style={styles.container}>            <div style={styles.topRow}>              <CornerButton color="red"/>              <CornerButton color="blue"/>            </div>            <div style={styles.middleRow}>              {this.props.squareColors.map((color, i) => <Square key={i} color={color}/>)}            </div>            <div style={styles.bottomRow}>              <CornerButton color="cyan"/>              <CornerButton color="green"/>            </div>          </div>      );  }}const mapDispatchToProps = { setColors }const mapStateToProps = state => {  return {    squareColors: state.colors.squareColors  }}export default connect(mapStateToProps, mapDispatchToProps)(App);角按鈕.js:import React from 'react';import { connect } from 'react-redux';import {setColors} from '../redux/actions';const styles = // some styles...class CornerButton extends React.Component {  updateSquaresColors = (color = null) => {    let squareColors = this.props.squareColors;    squareColors.unshift(color);    squareColors.pop();    this.props.setColors(squareColors);    localStorage.setItem('lastTenColors',  JSON.stringify(squareColors))  }
查看完整描述

1 回答

?
阿晨1998

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

我認(rèn)為 redux 無法識(shí)別狀態(tài)更改,因?yàn)閷?duì) squareColors 數(shù)組的引用保持不變,因此您必須傳遞該數(shù)組的副本,該副本將具有對(duì) setColors 方法的新引用。


      updateSquaresColors = (color = null) => {

        let squareColors = this.props.squareColors;

        squareColors.unshift(color);

        squareColors.pop();

        this.props.setColors([...squareColors]); //changed

        localStorage.setItem('lastTenColors',  JSON.stringify(squareColors))

      }


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

添加回答

舉報(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)