类型无效-预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:object

时间:2019-10-07 19:07:52

标签: reactjs typescript typeahead

警告:React.createElement:类型无效-预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到了:对象。

这是组件ContTypeAhead:

 <Typeahead
      id={this.props.name}
      name={this.props.name}
      onChange={this.changeHandler}
      onBlur={this.onBlurHandler}
      onInputChange={this.inputChangeHandler}
      options={this.state.typeAheadOptions}
      placeholder={'Type for ' + this.props.label}
      selected={this.state.selected}
    />

在其他组件和代码中使用上述组件是

    <ContTypeAhead
      label="Autex"
      name="autex"
      id="autex"
      size="6"
      options={[]}
      url="settlements/getAutex"
      selected={this.state.autex}
      changed={this.autexChangedHandler}
      wrongSelectionHandler={this.wrongTypeAheadSelectionHandler}
      rightSelectionHandler={this.rightTypeAheadSelectionHandler}
    />;

将TypeScript与CRA结合使用。定义的道具和状态界面,每个字段为:any;

2 个答案:

答案 0 :(得分:0)

您忘记将您的ContTypeAhead声明为
功能。
const ContTypeAhead = props => {
还是
类。
class ContTypeAhead extends Component {

或者您不return(<ContTypeAhead />)

答案 1 :(得分:0)

这确实是令人讨厌的问题,但我解决了。错误背后的原因是, Typeahead...,因此我们必须首先包括@types和最新版本的Typeahead。清楚地检查所使用的键,因为在新版本中,“ name”不是Typeahead的一部分。与日历相同。因此造成了问题。删除这些可以解决问题。

谢谢!