简体   繁体   English

React JS-如何重新渲染独立组件?

[英]React JS - How to rerender Independed component?

I am new to this React JS, please in this question. 我是这个React JS的新手,请问这个问题。 I Have 4 components(Insert,delete,update and show). 我有4个组件(插入,删除,更新和显示)。 when i insert data in Insert component then immediately it should show those details in Show components. 当我在插入组件中插入数据时,应立即在显示组件中显示那些详细信息。 Please find my sample code. 请找到我的示例代码。 Thanks in advance for your help. 在此先感谢您的帮助。

main.js : main.js:

import React from 'react';
import ReactDOM from 'react-dom';

import Insert from './Insert.js';
import Show from './Show.js';
import Delete from './Delete.js';
import Update from './update.js';

ReactDOM.render(<Insert/>, document.getElementById('Insert'))
ReactDOM.render(<Show />, document.getElementById('Show'))
ReactDOM.render(<Delete />, document.getElementById('Delete'))
ReactDOM.render(<Update/>, document.getElementById('Update'))

My Index.html : 我的Index.html:

  <table border="2" align="center">
  <tr>
        <td>
              <h1> Insert New Record </h1>
               <div id = "Insert"></div>
        </td>
        <td>
              <h1> Show Existing Records </h1>
              <div id = "Show"> </div>
        </td>
  </tr>
  <tr>
        <td>
              <h1> Delete Emp Record </h1>
              <div id="Delete"> </div>
        </td>
        <td>
              <h1> Update Existing Records </h1>
              <div id="update" > </div>
        </td>
  </tr>               
  <script src = "index.js"></script>

Now i need to refresh the Show component whenever i perform insert, update, delete operations. 现在,无论何时执行插入,更新,删除操作,都需要刷新Show组件。

My sample Insert Component : 我的示例插入组件:

import React from 'react';         
import Fetch from 'react-fetch';
import Show from './show'
class Insert extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      Id:0 ,
      name:'' ,
      mobile: '',
      email: '',
      dept : '',
      role :'',
      child : Show
    };

    this.handleSubmit = this.handleSubmit.bind(this);
     this.handleInputChange = this.handleInputChange.bind(this);
  }
   handleInputChange(event) {
      const target = event.target;
      const Id= target.Id;
      const name = target.name;
      const mobile = target.mobile;
      const email = target.email;
      const dept = target.dept;
      const role = target.role;

     this.setState({[Id]: event.target.value});
     this.setState({[name]: event.target.value});
     this.setState({[mobile]: event.target.value});
     this.setState({[email]: event.target.value});
     this.setState({[dept]: event.target.value});
     this.setState({[role]: event.target.value});    
  }

  handleSubmit(event) {

  let employee={
     Id:this.state.Id,
    name:this.state.name,
    mobile:this.state.mobile,
    email:this.state.email,
    dept:this.state.dept,
    role:this.state.role
    }
     fetch('http://localhost:25424/api/Employee/CreateNewEmployee/', {
    method: "POST",
    headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin' : 'http://localhost:7777' ,
    'Access-Control-Allow-Headers'  : 'Origin, X-Requested-With, Content-Type, Accept',
    'Access-Control-Allow-Methods' : 'POST'
    },
    body: JSON.stringify(employee)
    })
    .then(function(resp){

    })
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>

        <label >ID</label>
         <input type="number" name="Id" value={this.state.Id} onChange={this.handleInputChange}  />
         <br/>
         <label >name</label>
         <input type="text" name="name" value={this.state.name} onChange={this.handleInputChange}  />
        <br/>
         <label >Mobile</label>
         <input type="text" name="mobile"  value={this.state.mobile} onChange={this.handleInputChange} />
         <br/>
         <label >Email</label>
         <input type="text" name="email" value={this.state.email} onChange={this.handleInputChange} />
        <br/>
         <label >Dept</label>
         <input type="text" name="dept" value={this.state.dept} onChange={this.handleInputChange}  />
        <br/>
         <label >Role</label>
         <input type="text" name="role" value={this.state.role} onChange={this.handleInputChange}  />
        <br/>

        <input type="submit" value="Submit"   />

      </form>

    );
  }
}

export default Insert;

This is the file structure you need to aim for in a React app 这是您需要在React应用程序中定位的文件结构

- src/ - components/ - insert.js - delete.js - update.js - show.js - main.js - index.html

for example: 例如:

 main.js // should contain all the data(state) and should flow downwards to your child components
    import React, {Component} from 'react';
    import ReactDOM from 'react-dom';
    import 'insert' from /route/to/insert.js
    import 'delete' from /route/to/delete.js
    import 'show' from /route/to/show.js
    import 'update' from /route/to/update.js

    class Main extends Component {
      constructor(props) {
      super(props);
      this.state = {//initialize state object}
      }

      render() {
        return ( //jsx
           <table border="2" align="center">
           <tr>          
            <(insert.js component)/>
            <(delete.js component)/>
           </tr>
           <tr>          
            <(update.js component)/>
            <(show.js   component)/>
           </tr>
        )
      }
    }
    ReactDOM.reder(<Main/>, document.querySelector('one DOM element'))

and then... make each separate components to render out JSX into the main.js 然后...将每个单独的组件制作成将JSX渲染到main.js中

Hope that makes sense :/ 希望有道理:/

You dont have a parent child relationship in between you components. 您的组件之间没有父子关系。 You will need all of this in one parent React component, lets say Main.jsx. 假设Main.jsx是一个父React组件,就需要所有这些。 and it should look something like this: 它应该看起来像这样:

Main.jsx: Main.jsx:

import React from 'react';         
class Main extends React.Component {
  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.state = {
      flag: 0,
   }
 handleSubmit(){
    this.setState({flag : this.state.flag});
 }
 render() {
 return(<div>
     <Insert handleSubmit = {this.handleSubmit}/>
     <Show/>
     <Delete/>
     <Update />
    </div>);
  }
}
export default Main

You can put the structure of DOM or layout as per your desire in render. 您可以根据需要在渲染中放置DOM的结构或布局。 In handleSubmit function m reassigning the same state again so that it can re render. 在handleSubmit函数中,m再次重新分配相同的状态,以便可以重新呈现。 You can also use this.forceUpdate() for this but it is not advisable in some cases. 您也可以this.forceUpdate()使用this.forceUpdate() ,但在某些情况下不建议这样做。 Your handleSubmit function in Insert Component would look need to call the props function as this.props.handleSubmit() so that it can re render the main component which in turn will re render your show update and delete. 您在Insert Component中的handleSubmit函数看起来需要调用props函数,例如this.props.handleSubmit()以便它可以重新呈现主要组件,从而又可以呈现您的表演更新和删除。

Hope it helps. 希望能帮助到你。

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

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