代码之家  ›  专栏  ›  技术社区  ›  StudioTime

初始页面加载为空-创建react app

  •  0
  • StudioTime  · 技术社区  · 6 年前

    这就是我想要的:

    索引.js

    import React from 'react';
    import ReactDOM from 'react-dom';
    import './Assets/css/app.css';
    import { BrowserRouter } from 'react-router-dom';
    
    import App from './App';
    
    window.processIntent = intent => {
        ...cordova stuff
    };
    
    window.setupAltIntent = () => {
        ...cordova stuff
    };
    
    function startApp() {
        window.intentUrlToPrint = '';
        window.setupAltIntent();
    
        ReactDOM.render(
            <BrowserRouter>
                <App />
            </BrowserRouter>,
            document.getElementById('root')
        );
    }
    
    if (!window.cordova) {
        startApp();
    } else {
        document.addEventListener('deviceready', startApp, false);
    }
    

    应用程序js

    import React, { Component } from 'react';
    import AppActivity from './Views/activity/AppActivity';
    import Header from './Components/header';
    import AppRouter from './AppRouter';
    
    class App extends Component {
        componentDidMount() {
            if (navigator.splashscreen) {
                navigator.splashscreen.hide();
            }
        }
    
        render() {
            return (
                <div>
                    <div>
                        <Header />
                        <AppRouter />
                    </div>
                    <AppActivity />
                </div>
            );
        }
    }
    
    export default App;
    

    import React from 'react';
    import { Switch, Route } from 'react-router-dom';
    
    import Home from './Pages/Home';
    import Contact from './Pages/Contact';
    import LogInForm from './Pages/RegisterAndLogin/LogIn';
    
    const AppRouter = () => (
        <div>
            <Switch>
                <Route exact path="/" component={Home} />
                <Route path="/contact" component={Contact} />
                <Route path="/login" component={LogInForm} />
            </Switch>
        </div>
    );
    export default AppRouter;
    

    标题.js

    import React, { Component } from 'react';
    import HeaderNotLoggedIn from './Headers/HeaderNotLoggedIn';
    import HeaderLoggedIn from './Headers/HeaderLoggedIn';
    
    class Header extends Component {
        constructor(props) {
            super(props);
            this.isLoggedOn = this.isLoggedOn.bind(this);
        }
    
        isLoggedOn() {
            return localStorage.getItem('user');
        }
    
        render() {
            const header = this.isLoggedOn() ? <HeaderLoggedIn /> : <HeaderNotLoggedIn />;
            return <div>{header}</div>;
        }
    }
    
    export default Header;
    

    为什么应用程序只在页面刷新时正确加载?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Brooks Lybrand    6 年前

    我没有办法解决你的确切问题,因为有很多组件、逻辑和连接我无法访问,所以我不得不猜测。我想 setupAltIntent 返回一个承诺(尽管它做了其他事情并不完全重要)。在 App 我把它设置为有条件地呈现 Header 一旦数据加载到状态中,当 setState 在承诺得到解决后调用。

    链接到我的解决方案:

    Edit Initial page load empty - Solution