简体   繁体   English

在 React 中导出/导入文件 .jsx

[英]Export/import files .jsx in React

i recently tried to make separate files .jsx in React.我最近尝试在 React 中制作单独的文件 .jsx。 I made couple of them with export default name / import name from ./name.jsx' .我用export default name / import name from ./name.jsx'制作了几个。 But there comes problem.但是问题来了。 First i had imported Route and Links from react-router and console said it can't find Links , i found on stackoverflow to change it to react-router-dom , so i did it, and now console says i forgot to export some components .首先,我从react-router导入了 Route 和 Links ,控制台说它找不到 Links ,我在 stackoverflow 上发现将其更改为react-router-dom ,所以我做到了,现在控制台说我忘记导出一些组件. I can't find solution :( Could anyone help me ?我找不到解决方案:(有人可以帮助我吗?

Here's my project :这是我的项目:

https://github.com/tafarian/Portfolio https://github.com/tafarian/Portfolio

import React from 'react';
import ReactDOM from 'react-dom';
import './../css/style.scss';
import {
    Router,
    Route,
    Link,
    IndexLink,
    IndexRoute,
    hashHistory
} from 'react-router-dom';
import Template from './Template.jsx'
import Projects from './Projects.jsx'
import Home from './Home.jsx'
import AboutMe from './AboutMe.jsx'
import Contact from './Contact.jsx'

Such error as诸如此类的错误

"Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of App . “警告:React.createElement:类型无效——需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义。你可能忘记从它定义的文件中导出你的组件. 检查App的渲染方法。

means that you are trying to create an undefined component, and as it said in error - usually this happens when you forget to export component or the component that you are importing does not exist.意味着您正在尝试创建一个未定义的组件,并且正如它所说的错误 - 通常当您忘记导出组件或您正在导入的组件不存在时会发生这种情况。

When there is a lot of components and it's hard to find which one is "bad" I place a breakpoint at console.error(message);当有很多组件并且很难找到哪个是“坏的”时,我会在console.error(message);处放置一个断点console.error(message); in React code and go up the stacktrace to the function createElement and it's arguments.在 React 代码中,并在堆栈跟踪中找到函数createElement及其参数。 Usually it helps me to identify the problem.通常它可以帮助我确定问题。

The main problem, is that your code is not compatible with react-router@4.主要问题是您的代码与 react-router@4 不兼容。 In this version you can not pass children and component to Route at the same time.在此版本中,您不能同时将childrencomponent传递给Route Also, there is no such a thing as IndexRoute and you should use BrowserRouter instead of Router , or you should pass an history object.此外,没有IndexRoute这样的东西,你应该使用BrowserRouter而不是Router ,或者你应该传递一个history对象。

Take a look at the documention: https://reacttraining.com/react-router/web/guides/philosophy看一下文档: https : //reacttraining.com/react-router/web/guides/philosophy

And here is fixed version of yout app.jsx:这是你 app.jsx 的固定版本:

import React from 'react';
import ReactDOM from 'react-dom';
import './../css/style.scss';
import {
    BrowserRouter,
    Route,
    Link,
    hashHistory
} from 'react-router-dom';
import Template from './Template.jsx'
import Projects from './Projects.jsx'
import Home from './Home.jsx'
import AboutMe from './AboutMe.jsx'
import Contact from './Contact.jsx'

document.addEventListener('DOMContentLoaded', function(){

    class App extends React.Component {
        state = {
            loading: true,
        };

        componentDidMount() {
            setTimeout(() =>
                this.setState({
                    loading: false
                }), 2500);
        }

        render() {

            if(this.state.loading === true) {
                return (
                    <div id="preloader">
                        <div id="loader"></div>
                    </div>
                )
            }

            return(
                <BrowserRouter history={hashHistory}>
                    <Template>
                        <Route exact path="/" component={Home} />
                        <Route path='/aboutme' component={AboutMe} />
                        <Route path='/projects' component={Projects} />
                        <Route path='/contact' component={Contact} />
                    </Template>
                </BrowserRouter>
            )
        }
    }

    ReactDOM.render(
        <App />,
        document.getElementById('app')
    );
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM