react父子组件指的是什么-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

react父子组件指的是什么

这篇文章主要介绍了react父子组件指的是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇react父子组件指的是什么文章都会有所收获,下面我们一起来看看吧。

创新互联公司是一家集网站建设,方正企业网站建设,方正品牌网站建设,网站定制,方正网站建设报价,网络营销,网络优化,方正网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

在react组件的相互调用中,把调用者称为父组件,被调用者称为子组件。父子组件间可以传值:1、父组件向子组件传值时,先将需要传递的值传递给子组件,然后在子组件中,使用props来接收父组件传递过来的值;2、子组件向父组件传值时,需要通过触发方法来传递给父组件。

react父子组件指的是什么

本教程操作环境:Windows7系统、react18版、Dell G3电脑。

一、React中的组件

react组件就是自己定义的非html标签,规定react组件首字母大写

class App extends Component{
}

react父子组件指的是什么

二、父子组件

组件的相互调用中,把调用者称为父组件,被调用者称为子组件:

import React from 'react';
import Children from './Children';

class Up extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            
        }

    }

    

    render(){
        console.log("render");
        return(
            
                up                              
        )     } } export default Up;
import React from 'react';

class Children extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            
        }
    }
    
    render(){

        return (
            
                Children             
        )     } } export default Children;

三、父组件给子组件传值

父组件向子组件传值使用props。父组件向子组件传值时,先将需要传递的值传递给子组件,然后在子组件中,使用props来接收父组件传递过来的值。

父组件在调用子组件的时候定义一个属性:

这个值msg会绑定在子组件的props属性上,子组件可以直接使用:

this.props.msg

父组件可以给组件传值,传方法,甚至可以把自己传递给子组件

3.1 传值
import React from 'react';
import Children from './Children';

class Up extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            
        }

    }

    

    render(){
        console.log("render");
        return(
            
                up                              
        )     } } export default Up;
import React from 'react';

class Children extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            
        }
    }
    
    render(){

        return (
            
                Children                                  {this.props.msg}             
        )     } } export default Children;

react父子组件指的是什么

3.2 传方法
import React from 'react';
import Children from './Children';

class Up extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            
        }

    }

    run = () => {
        console.log("父组件run方法");
    }
    

    render(){
        console.log("render");
        return(
            
                up                              
        )     } } export default Up;
import React from 'react';

class Children extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            
        }
    }

    run = () => {
        this.props.run();
    }
    
    render(){

        return (
            
                Children                                  Run             
        )     } } export default Children;

react父子组件指的是什么

3.3 将父组件传给子组件
import React from 'react';
import Children from './Children';

class Up extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            
        }

    }

    run = () => {
        console.log("父组件run方法");
    }
    

    render(){
        console.log("render");
        return(
            
                up                              
        )     } } export default Up;
import React from 'react';

class Children extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            
        }
    }

    run = () => {
        console.log(this.props.msg);
    }
    
    render(){

        return (
            
                Children                                  Run             
        )     } } export default Children;

react父子组件指的是什么

四、子组件给父组件传值

子组件向父组件传值通过触发方法来传值

import React from 'react';
import Children from './Children';

class Up extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            
        }

    }

    getChildrenData = (data) => {
        console.log(data);
    }
    

    render(){
        console.log("render");
        return(
            
                up                              
        )     } } export default Up;
import React from 'react';

class Children extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            
        }
    }
    
    render(){

        return (
            
                Children                                   {this.props.upFun("子组件数据")}}>Run             
        )     } } export default Children;

react父子组件指的是什么

五、父组件中通过refs获取子组件属性和方法

import React from 'react';
import Children from './Children';

class Up extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            
        }

    }

    clickButton = () => {
        console.log(this.refs.children);
    }
    

    render(){
        console.log("render");
        return(
            
                up                                  click             
        )     } } export default Up; ``` ```js import React from 'react'; class Children extends React.Component{     constructor(props){         super(props);         this.state = {             title: "子组件"         }     }     runChildren = () => {              }          render(){         return (             
                Children                              
        )     } } export default Children; ``` ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200722065137748.png)

关于“react父子组件指的是什么”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“react父子组件指的是什么”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联行业资讯频道。


当前标题:react父子组件指的是什么
链接地址:http://kswsj.cn/article/jjiopj.html

其他资讯