反应选择默认值

当我将道具传递给它时,我有一个默认值为 null 的选择菜单,它会重新渲染新道具作为默认值


这是我的选择组件:


import React, { useState, useEffect } from "react";

 import Select from "react-select";


 class SelectMenu extends React.Component {

 state = {

  defaultValues: [],

  };


 componentWillReceiveProps(newProps) {

  this.setState({ defaultValues: newProps.defaultValue });

  }


  render() {

 return (

  <Select

    options={this.props.options}

    closeMenuOnSelect={this.props.closeMenuOnSelect}

    components={this.props.components}

    isMulti={this.props.isMulti}

    onChange={(e) => this.props.onChange(e, this.props.nameOnState)}

    placeholder={this.props.default}

    defaultValue={this.state.defaultValues}

  />

  );

 }  

 }


 export default SelectMenu;


慕尼黑的夜晚无繁华
浏览 110回答 3
3回答

白猪掌柜的

我找到了解决这个问题的方法,方法是使用组件将接收道具并使用即将到来的道具设置我的状态,并且在渲染中你需要做条件来渲染选择菜单只有当 state.length !== 0我发布了这个答案以防万一有人遇到同样的问题我知道它不是最佳解决方案但它对我有用

芜湖不芜

componentWillReceiveProps在安装期间不会被调用。React 在安装期间不会使用初始道具调用 UNSAFE_componentWillReceiveProps()。如果某些组件的道具可能会更新,它只会调用此方法。( https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops )此外,componentWillReceiveProps已弃用并将在 React 17 中删除。getDerivedStateFromProps改为查看,尤其是关于何时不需要它的注释。我相信在您的情况下使用构造函数会非常好,例如:class Components extends React.Component {&nbsp; &nbsp;constructor(props) {&nbsp; &nbsp; &nbsp; super(props)&nbsp; &nbsp; &nbsp; this.state = { some_property: props.defaultValue }&nbsp; &nbsp;}}

泛舟湖上清波郎朗

对之前的解决方案感到抱歉,但它不是最佳的我找到了一种方法让它工作所以你必须将它作为值道具而不是默认值,如果你想将删除和添加的值捕获到你的默认值这个函数将帮助你很多&nbsp; &nbsp; &nbsp; onChange = (e) => {&nbsp; &nbsp;if (e === null) {&nbsp; &nbsp;e = [];&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;this.setState({&nbsp; &nbsp; &nbsp; &nbsp; equipments: e,&nbsp; });&nbsp; let added = e.filter((elm) => !this.state.equipments.includes(elm));&nbsp; if (added[0]) {&nbsp; let data = this.state.deletedEquipments.filter(&nbsp; &nbsp; (elm) => elm !== added[0].label&nbsp; );&nbsp; &nbsp;this.setState({&nbsp; &nbsp; deletedEquipments: data,&nbsp; &nbsp; });&nbsp; }&nbsp; let Equipments = e.map((elm) => elm.label);&nbsp; let newEquipments = Equipments.filter(&nbsp; &nbsp; (elm) => !this.state.fixed.includes(elm)&nbsp; );&nbsp; this.setState({&nbsp; &nbsp;newEquipments: newEquipments,&nbsp; });&nbsp; &nbsp;let difference = this.state.equipments.filter((elm) => !e.includes(elm));&nbsp; &nbsp;if (difference.length !== 0) {&nbsp; &nbsp;if (&nbsp; &nbsp; &nbsp;!this.state.deletedEquipments.includes(difference[0].label) &&&nbsp; &nbsp; &nbsp;this.state.fixed.includes(difference[0].label)&nbsp; &nbsp; &nbsp;) {&nbsp; &nbsp; &nbsp;this.setState({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;deletedEquipments: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...this.state.deletedEquipments,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;difference[0].label,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;],&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; };&nbsp; constructor(props) {&nbsp; &nbsp;super(props);&nbsp; &nbsp;this.state = {&nbsp; &nbsp; equipments: [],&nbsp; &nbsp; newEquipments: [],&nbsp; &nbsp;deletedEquipments: [],&nbsp; };&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript