如何在reactjs中将html元素从子元素传递给父元素?

时间:2021-06-15 03:50:14

标签: html reactjs components parent-child

<块引用>

我创建了 2 个组件。欢迎和Datecomp分开。但我希望作为父母和 Datecomp 作为孩子受到欢迎。但我不知道如何将数据从孩子传递给父母

我创建了 2 个组件。欢迎和Datecomp分开。但我希望作为父母和 Datecomp 作为孩子受到欢迎。但我不知道如何将数据从孩子传递给父母

Welcome.js
import React, { Component } from 'react';
class Welcome extends Component {
render(){
    return(
            <div>
                <h1>Welcome User</h1>
                <p>What is React? React is a declarative,efficient, and flexible 
                JavaScript library for <br />
                building user interfaces. It lets you compose complex UIs 
                from small and <br />isolated pieces of code called "components".
                </p>
            </div>
            );
    } 
}
class DateComp extends Component {
constructor(){
  super()
  var today = new Date(),
  date = today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear();
  this.state={
      currentDate:date
  }
 }
 render(){
  return(
      <div style={{float: "right"}}> 
             
      Dated:
      {this.state.currentDate}
      </div>
      );
} 
}


export default Welcome;
export {DateComp};

index.js
import React from 'react';
import ReactDOM from 'react-dom';

import Welcome,{DateComp} from './Welcome';

ReactDOM.render(
<>
<Welcome/>
<DateComp/>
</>,
document.getElementById('root'),
);

0 个答案:

没有答案