在React中无法正确呈现组件,它们会在浏览器刷新上呈现

时间:2019-01-12 15:18:01

标签: javascript reactjs react-router react-router-dom

我已经设置了App.js,将我的组件链接到他们的页面。一切正常。单击时页面已更改。我也需要为不同的页面更改标题,因此我创建了一个新的Route文件,如App.js,但随后又为我的标题创建了Headers.js。该文件包含我在所有页面上可以拥有的所有可能的标题。我还在我的index.js中链接了此文件。现在,我的所有标题都按了更改,就像应该的那样,但是现在我的实际内容(应用程序本身)却没有。当使用刷新动作时,它会改变,这不是应该的方式。当我按下链接时,我希望一切都发生变化,而不仅仅是标题。在我的index.html文件中,指定了<div class="header"><div class="root">

标题确实会像预期的那样变化 App.js的内容没有,刷新页面后会更改。


index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Headers from './components/Header/Headers';
import App from './components/App/App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(<Headers />, document.getElementById('header'));
ReactDOM.render(<App />, document.getElementById('root'));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: bitlyshortenedURL i had to delete in stackoverflow cus of errors
serviceWorker.unregister();

App.js

//dependencies
import React, { Component } from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';

//components
import Footer from '../Footer/Footer';
import Home from '../Pages/Home';
import StudentOverview from '../Pages/StudentOverview';
import StudentsPerClass from '../Pages/StudentsPerClass';
import StudentsPerSubject from '../Pages/StudentsPerSubject';
import StudentDetails from '../Pages/StudentDetails';
import Management from '../Pages/Management';
import StudentAdd from '../Pages/StudentAdd';
import Exercise from '../Exercise/Exercise';
import NameWithFaceMC from '../Exercise/NameWithFaceMC';
import NameWithFace from '../Exercise/NameWithFace';
import FaceWithName from '../Exercise/FaceWithName';

//includes
import '../../public/css/kdg-fonts.css';
import '../../public/css/normalize.css';
import '../../public/css/responsive.css';
import '../../public/css/say-my-name.css';
import '../../public/css/style.css';
import '../../public/css/style.min.css';
import '../../public/js/main.js';

//Run
class App extends Component {
  render() {
    return (
      <Router>
        <div className="App">
          <Route exact path='/' component={Home} />
          <Route exact path='/studenten' component={StudentOverview} />
          <Route exact path='/studentenperklasgroep' component={StudentsPerClass} />
          <Route exact path='/studentenpervak' component={StudentsPerSubject} />
          <Route exact path='/studenten/detail/:id' component={StudentDetails} />
          <Route exact path='/beheer' component={Management} />
          <Route exact path='/beheer/add' component={StudentAdd} />
          <Route exact path='/oefenen' component={Exercise} />
          <Route exact path='/oefenen/nbgmc' component={NameWithFaceMC} />
          <Route exact path='/oefenen/nbg' component={NameWithFace} />
          <Route exact path='/oefenen/gbn' component={FaceWithName} />
          <Route path='*' component={Footer} />
        </div>
      </Router>
    );
  }
}

export default App;

package.json

{
  "name": "saymyname",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "^2.1.3"
  },
  "scripts": {
    "dev": "webpack --mode development",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "gulp": "^4.0.0",
    "gulp-changed": "^3.2.0",
    "gulp-clean-css": "^4.0.0",
    "gulp-rename": "^1.4.0",
    "gulp-sass": "^4.0.2",
    "gulp-uglify": "^3.0.1"
  }
}

添加了我在浏览器中获得的视频: https://i.gyazo.com/99daa742967947c1e779917bd18a1182.mp4 就像我说的那样,该页面显示了应刷新显示在浏览器中的内容。

如果需要更多代码,屏幕截图或视频,请告诉我,我会提供。预先感谢

1 个答案:

答案 0 :(得分:0)

我认为问题在于方法:您刚刚在index.js中所做的是实例化两个不同的React应用程序,这是错误的。

您应该只有一个应用程序入口点(#root div中的App.js),并将Header更改的逻辑放入其中。

有很多方法可以实现您要执行的操作,但这基本上取决于您的标头彼此之间的差异。

例如,如果文本或链接有什么变化,您可以创建一个通用的Header组件,将其导入到页面中并将文本,链接数组或其他可能需要作为道具的东西传递给它。