为什么textarea会渲染,但具有相同参数的AutosizeTextarea会导致不变违规异常?

时间:2016-04-13 19:47:50

标签: reactjs

我在React中有一个工作textarea,我想修改rows属性。这是文本区域:

   <textarea
        style={{width: '100%'}}
        type="text"
        value={this.state.description}
        onChange={this.changeDescription}
    />

设置rows={x}有效。

但现在我希望它能随着我的打字而增长。我没有编写自己的解决方案,而是尝试使用var AutosizeTextarea = require('react-textarea-autosize');

有趣的是,即使我在开头传递相同的参数(根据文档),组件应该接受这些参数,因为这些参数在example中使用

然而,当我在我的JSX中使用它时,这样:

<AutosizeTextarea
    style={{width: '100%'}}
    value={this.state.description}
    onChange={this.changeDescription}
/>

我收到以下错误:

Warning: React.createElement: type should not be null, undefined,
boolean, or number. It should be a string (for DOM elements) or a
ReactClass (for composite components).
Check the render method of `NewProblemPageContainer`.

Debug: internal, implementation, error
Invariant Violation: Uncaught error: Element type is invalid:
expected a string (for built-in components) or a class/function
(for composite components) but got: object.
Check the render method of `NewProblemPageContainer`.

我真的对此错误感到困惑,因为我这样做:

 componentWillMount = () => {
    this.setState({
     description: '',
    });
 }

onChange参数是一个函数:

changeDescription = (event) => {
    this.setState({
            description: event.target.value
    });
};

当我传递的两个参数符合错误说明的条件时,我需要对此组件进行更改(其添加会导致异常发生):期望字符串(对于内置组件)或类/函数(对于复合组件)但得到:object。

我不会理解这会如何改变我的render()方法,所以如果它与此有关,请求解释!我在我脑海里,

1 个答案:

答案 0 :(得分:0)

可能你现在不需要这个,但对于在得到相同错误后发现这个的人,请参阅:

https://github.com/andreypopp/react-textarea-autosize/issues/98

你需要使用

var AutosizeTextarea = require('react-textarea-autosize').default;

如果使用CommonJS需要语法,或

import AutosizeTextarea from 'react-textarea-autosize';

如果使用ES6导入语法。