自定义组件未在此基本 ReactJS 应用程序上正确呈现

IT技术 javascript reactjs redux react-redux
2021-05-17 12:09:53

我有一个非常基本的ReactJS应用程序,它使用Redux包含以下组件:

PanelMaterialSize > Select

/src/controls/PanelMaterialSize/PanelMaterialSize.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import './PanelMaterialSize.scss';
import Select from '../Select/Select';
import { setThemeList } from '../../store/AppConfig/actions';

class PanelMaterialSize extends Component {

  componentDidMount() {
    this.n = 1;
    setInterval(() => {
      let themeList = [
        { value: this.n, text: 'Option ' + this.n },
        { value: this.n + 1, text: 'Option ' + (this.n + 1) },
        { value: this.n + 2, text: 'Option ' + (this.n + 2) },
      ];
      this.props.setThemeList(themeList);
      this.n += 3;
    }, 1000);
  }

  render() {
    return (
      <div className="partial-designer-panel-material-size">
        <div>
          <div className="label-input">
            <div className="label">MATERIAL</div>
            <div className="input">
              <Select data={this.props.themeList} style={{ width: '100%' }} />
            </div>
          </div>
        </div>
      </div>
    );
  }

}

const mapStateToProps = (appState) => {
  return {
    themeList: appState.appConfig.themeList,
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    setThemeList: (themeList) => dispatch(setThemeList(themeList)),
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(PanelMaterialSize);

在我看来,Redux逻辑很好,因为我已经通过做几件事进行了测试。

我的问题是,当render(...)方法 of:PanelMaterialSize被调用时,组件:Select不会使用新数据(每秒钟更改一次)进行渲染。

在这里你有一个Codesandbox.io你可以玩的(最好使用Chrome):

https://codesandbox.io/s/03mj405zzv

关于如何正确更改其内容的任何想法?

如果可能,请提供一个新Codesandbox.io的解决方案,从前一个分叉出来。

谢谢!

1个回答

问题出在您的选择组件中。您最初传递的是空数组并使用 this.state.data props检查您的组件,下次减速器更改您的 this.state.data 将不会更新数据。因为你在构造函数中初始化。构造函数只在组件挂载时调用一次。

已解决的演示链接

问题出在您的选择渲染方法中:

  render() {
    let data = this.state[this.name];
    return (
      <div className="control-select" {...this.controlProps}>
        <div className="custom-dropdown custom-dropdown--grey">
          <select className="custom-dropdown__select custom-dropdown__select--grey">
             //change data with this.props.data
            {this.props.data.length > 0 &&
              this.props.data.map((elem, index) => {
                return (
                  <option value={elem.value} key={index}>
                    {elem.text}
                  </option>
                );
              })}
          </select>
        </div>
      </div>
    );
  }