react中什么是父子组件

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

react中什么是父子组件

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

一、React中的组件

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

class App extends Component{}

登录后复制

1.png

二、父子组件

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

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;

登录后复制

在这里插入图片描述

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
) }}export default Children;

登录后复制

在这里插入图片描述

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
) }}export default Children;

登录后复制

2.png

四、子组件给父组件传值

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

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
) }}export default Children;

登录后复制

在这里插入图片描述

五、父组件中通过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
) }}export default Up;``````jsimport 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?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldGlhbnhm,size_16,color_FFFFFF,t_70)

登录后复制

【相关推荐:Redis视频教程】

以上就是react中什么是父子组件的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2927991.html

(0)
上一篇 2025年3月11日 19:30:48
下一篇 2025年3月7日 05:01:05

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • react新旧生命周期的区别是什么

    react新旧生命周期的区别:1、新生命周期中去掉了三个will钩子,分别为componentWillMount、componentWillReceiveProps、componentWillUpdate;2、新生命周期中新增了两个钩子,分…

    2025年3月11日
    200
  • react有哪些插件

    react插件有:1、Redux,提供可预测化的状态管理;2、MobX,通过函数响应式编程使得状态管理变得简单和可扩展;3、Redux Thunk,Redux的异步处理中间件;4、Dva,一个基于redux和redux-saga的数据流方案…

    2025年3月11日
    200
  • vue与react属于什么框架

    vue与react属于JavaScript框架。vue是一套用于构建用户界面的渐进式JavaScript框架,Vue的核心库只关注视图层,并且非常容易学习,非常容易与其它库或已有项目整合。react是一个用来构建用户界面的JavaScrip…

    2025年3月11日
    200
  • react组件首字母必须大写吗

    react组件首字母必须大写;因为React根据首字母是否大写来区分是react组件还是dom元素。React中使用JSX语法,但浏览器无法识别JSX语法,需通过babel对JSX语法进行转义;而如果组件的首字母为小写时,其会被认定为原生D…

    2025年3月11日 编程技术
    200
  • react用什么ui组件库

    react可用ui组件库有:1、Material-UI,有用于布局、表单、导航、数据显示和许多其他小部件的组件;2、Ant Design,有60多个组件,带有可预测的静态类型;3、Blueprint,有40多个组件;4、React Boot…

    2025年3月11日 编程技术
    200
  • react为什么要用合成事件

    react使用合成事件主要有三个目的:1、进行浏览器兼容,实现更好的跨平台;React提供的合成事件可用来抹平不同浏览器事件对象之间的差异,将不同平台事件模拟合成事件。2、避免垃圾回收;React事件对象不会被释放掉,而是存放进一个数组中,…

    2025年3月11日 编程技术
    200
  • react.js是用es6写的吗

    react.js是用es6写的,可以用Babel转译为ES5,也可以用Babel转译为JavaScript的JSX;由于React的设计思想极其独特,属于革命性创新,性能出众,代码逻辑却非常简单。使用ES6来创建组件的语法更加简洁,这种语法…

    2025年3月11日
    200
  • react是不是Spa

    react是Spa模式,即组件嵌套,其主要的传参方式有:1、在挂载的地方给组件传参;2、父子传参,即用props或ref方式传参;3、子父传参,即子组件通过触发父组件的方法来改变父组件的状态;4、兄弟传参,即通过改变公有组件的状态进而改变子…

    2025年3月11日
    200
  • ie无法识别react怎么办

    ie无法识别react的解决办法:1、安装“react-app-polyfill”,并在“src/main.js”中引入;2、在“babel.config.js”中配置参数;3、在“vue.config.js”中配置“transpileDe…

    2025年3月11日 编程技术
    200
  • react更新state方法有哪些

    react更新state方法有:1、通过key变化子组件,代码如“”;2、利用ref父组件调用子组件函数;3、通过父级给子级传数据,子级只负责渲染。 本教程操作环境:Windows7系统、react17.0.1版、Dell G3电脑。 re…

    2025年3月11日
    200

发表回复

登录后才能评论