React 无法将 undefined 或 null 转换为对象

React can not convert undefined or null to object

我正在使用 react-select 并且我有两个多 select 下拉元素。我已声明我的状态如下:

constructor(props){

    super(props);

    this.state = {
    createNewDeliveryData: {
            languages: [],
            markets:[],

        },

    };

} 

我有两个函数可以从我的 multi select 下拉列表中添加 languages/markets selected:

addLanguage = (val) => {
    // Get a copy of the languages state.
    const languages = [...this.state.createNewDeliveryData.languages];
    for(let i=0; i< val.length; i++){
        if(languages.indexOf(val[i].id) === -1) { // notice that there is a parenthesis after `id`.
            languages.push(val[i].id)
        }
    }
    this.setState(
        {
            createNewDeliveryData: {languages}
        }, function () {
            this.setValue({id:"languages", value:this.state.createNewDeliveryData.languages, error:null})
        });
}

addMarket = (value) => {

    console.log(value)
    // Get a copy of the markets state.
    const markets = [...this.state.createNewDeliveryData.markets];
    for(let i=0; i< value.length; i++){
        if(markets.indexOf(value[i].id) === -1) { // notice that there is a parenthesis after `id`.
            markets.push(value[i].id)
        }
    }
    this.setState(
        {
            createNewDeliveryData: {markets}
        }, function () {
            this.setValue({id:"markets", value:this.state.createNewDeliveryData.markets, error:null})
        });
}

我的 select 个邮箱:

{/* Language */}
<Row>
    <Col xs={3} sm={3} md={2} lg={2}>
        <div className="test-one">
            <FormattedMessage id="language" defaultMessage="Sprache der Lieferung" />
        </div>
    </Col>
    <Col xs={6} sm={6} md={6} lg={6}>

        <Select
            multi={true}
            name="form-field-name"
            options={this.props.metadata ? this.props.metadata["latest"].languages : []}
            labelKey="display_name"
            valueKey="id"
            onChange={this.addLanguage}
            clearable={false}
            value={this.state.createNewDeliveryData.languages}
        />
    </Col>
</Row><br/>

{/* Target market */}
<Row>
    <Col xs={3} sm={3} md={2} lg={2}>
        <div className="test-one">
            <FormattedMessage id="market" defaultMessage="Zielmarkt" />
        </div>
    </Col>
    <Col xs={6} sm={6} md={6} lg={6}>
        <Select
            multi={true}
            name="form-field-name"
            options={this.props.metadata ? this.props.metadata["latest"].countries : []}
            labelKey="display_name"
            valueKey="id"
            onChange={this.addMarket}
            clearable={false}
            value={this.state.createNewDeliveryData.markets}
        />
    </Col>
</Row><br/>

我通过调用后端来填充我的下拉菜单。现在的问题是我可以从任一框中 select,当我从其他框中选择一个值时,它抛出错误提示:无法将未定义或 null 转换为对象。

为什么会这样?我做错了什么?

问题在于您更新状态值的方式。

最初 createNewDeliveryData 包含两个键:

createNewDeliveryData = {
   languages: [],
   markers: []
}

但是当您更新状态时,您将删除第二个状态:

this.setState({
    createNewDeliveryData: {languages}
    ...
})

意思是:

createNewDeliveryData: {languages: languages}

现在 createNewDeliveryData 将只有一种关键语言,而不是另一种。

要解决此问题,请像这样更新状态:

this.setState({
        createNewDeliveryData: {...this.state.createNewDeliveryData , languages:languages }
    }, function () {
          this.setValue({id:"languages", value:this.state.createNewDeliveryData.languages, error:null})
    });
}

检查这个片段:

//problem

let obj1 = {a:1, b:2};
console.log('initially obj1 = ', obj1);
let b = 4;
obj1 = {b};

console.log('after obj1 = ', obj1);

//solution

let obj2 = {a:1, b:2};
console.log('initially obj2 = ', obj2);
b = 4;
obj2 = {...obj2, b};

console.log('after obj2 = ', obj2);