React Native-参考似乎未定义

时间:2018-07-26 07:18:51

标签: react-native

有人可以在这段代码中为我提供“ ref”帮助吗?在最后一个“视图”组件中,似乎“ this.multiSelect”未定义。我不明白为什么以及在哪里犯错。它与上下文有关吗?

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import MultiSelect from 'react-native-multiple-select';

export default class MultiSelectComponent extends Component {

  constructor() {
    super();
    this.state = {
      selectedItems : []
    };

    this.items = [{
      id: '92iijs7yta',
      name: 'Ondo',
    }, {
      id: 'suudydjsjd',
      name: 'Abuja',
    }];
  }

  onSelectedItemsChange = selectedItems => {
    this.setState({ selectedItems });
  };

  render() {
    const { selectedItems } = this.state;
    return (
      <View style={{ flex: 1 }}>
        <MultiSelect
            hideTags
            items={this.items}
            uniqueKey="id"
            ref={(component) => { this.multiSelect = component; }}
            onSelectedItemsChange = {this.onSelectedItemsChange}
            selectedItems={selectedItems}
            selectText="Pick Items"
            searchInputPlaceholderText="Search Items..."
            submitButtonText="Submit"
        />
        <View>
            {this.multiselect
            ? 
            this.multiselect.getSelectedItemsExt(selectedItems)
            :
            null}
        </View>
      </View>
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您的代码中有错字,可能是错误所在: 您正在使用this.multiselect并声明

ref={(component) => { this.multiSelect = component; }}

所以也许是这样:

ref={(component) => { this.multiselect = component; }}