简体   繁体   English

反应组件未呈现内容

[英]React Component Not Rendering the content

React router not rendering my JSX. React 路由器没有渲染我的 JSX。 The initial page renders the content inside.初始页面呈现里面的内容。 When I click add employee it doesn't display the content of the components.当我单击添加员工时,它不显示组件的内容。 It renders a blank page.它呈现一个空白页面。 I'm a beginner in React.我是 React 的初学者。

CreateEmployeeComponent.jsx renders a blank page. CreateEmployeeComponent.jsx 呈现一个空白页面。

Below is the code of each file.下面是每个文件的代码。 Thanks in advance提前致谢

App.js应用程序.js

import './App.css';
import React from 'react';
import ListEmployeeComponent from './components/ListEmployeeComponent';
import CreateEmployeeComponent from './components/CreateEmployeeComponent';
import HeaderComponent from './components/HeaderComponent';
import FooterComponent from './components/FooterComponent';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'

function App() {
  return (
    <div>
      <Router>
        <div>
          <HeaderComponent />
          <div className="container">
            <Switch>
              <Route path="/" exact component={ListEmployeeComponent}></Route>
              <Route path="/employees" component={ListEmployeeComponent}></Route>
              <Route path="/add-emplyee" component={CreateEmployeeComponent}></Route>
            </Switch>
          </div>
          <FooterComponent />
        </div>
      </Router>
    </div>

  );
}

export default App;

ListEmployeeComponent ListEmployee组件

import React, { Component } from 'react';
import EmployeeService from '../services/EmployeeService';

class ListEmployeeComponent extends Component {
    constructor(props) {
        super(props)

        this.state = {
            employees: []
        }

        this.addEmployee = this.addEmployee.bind(this);
    }

    componentDidMount() {
        EmployeeService.getEmployees().then((res) => {
            this.setState({ employees: res.data });
        })
    }

    addEmployee() {
        this.props.history.push('/add-employee');

    }

    render() {
        return (
            <div>
                <h2 className="text-center">Employees List</h2>
                <div className="row">
                    <button className="btn btn-primary" onClick={this.addEmployee}>Add Employee</button>
                </div>
                <div className="row">
                    <table className="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>Employee First Name</th>
                                <th>Employee Last Name</th>
                                <th>Employee Email Id</th>
                                <th>Actions</th>
                            </tr>
                        </thead>

                        <tbody>
                            {
                                this.state.employees.map(
                                    employee =>
                                        <tr key={employee.id}>
                                            <td> {employee.firstName} </td>
                                            <td> {employee.lastName} </td>
                                            <td> {employee.emailId} </td>

                                        </tr>
                                )
                            }
                        </tbody>

                    </table>

                </div>

            </div>
        );
    }
}

export default ListEmployeeComponent;

CreateEmployeeComponent创建员工组件

import React, { Component } from 'react';

class CreateEmployeeComponent extends Component {
    
    render() {
        return (
            <div>
               <h1>Create Employee...</h1>
            </div>
            
        );
    }
}

export default CreateEmployeeComponent;

You got type mistake: /add-emplyee -> /add-employee.您输入错误:/add-emplyee -> /add-employee。

      <Route path="/add-emplyee" component={CreateEmployeeComponent}></Route>

Please check it first请先检查

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

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