简体   繁体   English

我在导出组件时丢失了什么吗?

[英]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

Probably, you have wrong import paths. 可能是您的导入路径错误。 Here is the demo written using your code snippets. 是使用您的代码段编写的演示。 It's working without any concerns. 它的工作没有任何顾虑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 JavaScript 代码中是否遗漏了一些我可能会忽略的内容? - Am I missing something in my JavaScript code that I may be overlooking? 引导程序干扰了我的Jqueryui还是我缺少了什么? - Is bootstrap interfering with my Jqueryui or am i missing something? 我的signalR客户端缺少什么基本组件? - What basic component am I missing from my signalR client? 为什么我的悬停添加类在Jsfiddle上有效,但在我的网站上却不可用? 我的标题中是否缺少某些内容? - Why is my add class on hover working on Jsfiddle but not on my website? Am I missing something in my header? 我想念什么吗? 我的值不会存储到我的数组中 - Am I missing something? My values don't get stored into my array 我忽略了快捷方式编程语言? - Shortcut programming languages am I missing something? setTimeout不起作用,我在这里错过了什么吗? - setTimeout not working,am I missing something here? Javascript indexOf并替换-我缺少什么吗 - Javascript indexOf and replace - am I missing something WebGL 多边形偏移量中是否存在错误或者我遗漏了什么? - Is there a bug in WebGL polygonOffset or am I missing something? 动态导入:我错过了什么吗? - Dynamic Imports: Am I missing something?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM