如何修复 Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) 但得到:对象
How to fix Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object
我正在尝试 运行 ReactRails 应用程序并尝试 运行 一个非常简单的 react-select 组件。但是,在同一个文件中,如果我只打印一个简单的 h2
元素,它可以工作,但 <Select/>
不起作用。它给出:
Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
我正在尝试使用 react-select
组件。我已经通过 yarn add
命令安装了它。
User.jsx:
var React = require("react")
var Select = require("react-select")
var PropTypes = require("prop-types")
// also tried these ->
// import React from 'react';
// import createClass from 'create-react-class';
// import PropTypes from 'prop-types';
// import Select from 'react-select';
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];
class User extends React.Component {
state = {
selectedOption: null,
}
handleChange = (selectedOption) => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;
/*
return (
<h2>THIS WORKS!</h2>
)
*/
return (
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
)
// Doesn't work:
// Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
}
}
module.exports = User
我是 React 世界的新手。我在这里错过了什么?我做错了什么?
注意:这并没有解决我的问题:
我不确定 module.exports 如何与 React 一起玩。当我用 ES6 语法尝试你的代码时,它似乎工作正常。
import React from 'react';
import Select from 'react-select';
然后使用 ES6 导出而不是 module.exports:
export default User;
Select 组件似乎不是问题所在。
我正在尝试 运行 ReactRails 应用程序并尝试 运行 一个非常简单的 react-select 组件。但是,在同一个文件中,如果我只打印一个简单的 h2
元素,它可以工作,但 <Select/>
不起作用。它给出:
Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
我正在尝试使用 react-select
组件。我已经通过 yarn add
命令安装了它。
User.jsx:
var React = require("react")
var Select = require("react-select")
var PropTypes = require("prop-types")
// also tried these ->
// import React from 'react';
// import createClass from 'create-react-class';
// import PropTypes from 'prop-types';
// import Select from 'react-select';
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];
class User extends React.Component {
state = {
selectedOption: null,
}
handleChange = (selectedOption) => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
}
render() {
const { selectedOption } = this.state;
/*
return (
<h2>THIS WORKS!</h2>
)
*/
return (
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
)
// Doesn't work:
// Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
}
}
module.exports = User
我是 React 世界的新手。我在这里错过了什么?我做错了什么?
注意:这并没有解决我的问题:
我不确定 module.exports 如何与 React 一起玩。当我用 ES6 语法尝试你的代码时,它似乎工作正常。
import React from 'react';
import Select from 'react-select';
然后使用 ES6 导出而不是 module.exports:
export default User;
Select 组件似乎不是问题所在。