[英]Am I missing something with exporting my component?
hope you can provide some fresh eyes for something I probably missed out/overlooked. 希望您可以为我可能错过/忽略的某些事物提供新鲜的视线。 I am currently creating a weather app with React and since adding in Navbar component, I got the error: "Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. 我目前正在使用React创建一个天气应用,并且由于添加了Navbar组件,因此出现错误:“元素类型无效:期望使用字符串(对于内置组件)或类/函数(对于复合组件),但得到:您可能忘记了从定义文件中导出组件,或者您可能混淆了默认导入和命名导入。
Check the render method of App
." 检查App
的渲染方法。”
However, I did export my components and imported them in index.js and app.js...I'm wondering what I possibly overlooked? 但是,我确实导出了组件并将其导入到index.js和app.js中...我想知道我可能忽略了什么? Thank you for the help! 感谢您的帮助!
I've looked over every component created to see if I missed out on exporting any components, but they are all fine. 我检查了创建的每个组件,以查看是否错过了导出任何组件的操作,但是它们都很好。 I also checked to see if maybe I'm not importing from the right paths or folders when I try importing them, but they are fine. 我还检查了尝试导入它们时是否可能没有从正确的路径或文件夹中进行导入,但是它们很好。 I'm thinking there's something either wrong with my Navbar component or App component. 我在想我的Navbar组件或App组件有问题。
//This is my Navbar Comp
import React, { Component } from 'react';
import './Navbar.css';
import SearchBox from '../SearchBox';
import UnitComponent from '../UnitComponent';
class Navbar extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<nav>
<ul className="navbar-container">
<li className="navbar-list-item">
<SearchBox />
</li>
<li className="navbar-list-item city-name">
<span className="">New York,US</span>
</li>
<li className="navbar-list-item">
<UnitComponent />
</li>
</ul>
</nav>
);
}
}
export default Navbar;
//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './style.css';
import App from './components/App';
const ROOT_NODE = document.getElementById('root');
ReactDOM.render(<App />, ROOT_NODE);
//app.js
import React, { Component } from 'react';
import Navbar from '../Navbar';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div>
<span><i className="fab fa-react"></i></span>
<span>Weather App</span>
<span><i className="wi wi-day-sunny"></i></span>
<Navbar />
</div>
)
}
}
export default App;
//SearchBox Component
import React, { Component } from 'react';
import './SearchBox.css';
class SearchBox extends Component {
constructor(props) {
super(props);
this.state = {
queryString: ''
};
}
handleQueryStringChange = (e) => {
this.setState({
queryString: e.target.value
})
}
handleSearch = (e) => {
e.preventDefault();
console.log('Fetch weather data for:', this.state.queryString);
}
render() {
return (
<div className="form-container">
<form>
<input
type="search"
value={this.state.queryString}
name="searchBox"
id="searchBox"
placeholder="Enter City or Zipcode" />
<span className="search-button fa fa-search"></span>
</form>
</div>
);
}
}
export default SearchBox;
//UnitComponent
import React, { Component } from 'react';
import './UnitComponent.css';
class UnitComponent extends Component {
constructor(props) {
super(props);
this.state = {
unit: 'C'
};
}
changeUnit = (e) => {
const newUnit = e.target.textContent;
this.setState({
unit: newUnit
})
}
render() {
return (
<div className="unit-container">
<span className={`unit-value ${this.state.unit === 'C' ? 'active-unit' : ''}`} onClick={this.changeUnit}>C</span>
<span className={`unit-value ${this.state.unit === 'F' ? 'active-unit' : ''}`} onClick={this.changeUnit}>F</span>
</div>
)
}
}
export default UnitComponent;
[![Folder structure][1]][1]
[1]: https://i.stack.imgur.com/UWcyc.png
^^in each component folder, there are files such as Navbar.css, Navbar.jsx, index.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.