在react组件的相互调用中,把调用者称为父组件,被调用者称为子组件。父子组件间可以传值:1、父组件向子组件传值时,先将需要传递的值传递给子组件,然后在子组件中,使用props来接收父组件传递过来的值;2、子组件向父组件传值时,需要通过触发方法来传递给父组件。
本教程操作环境:Windows7系统、react18版、Dell G3电脑。
一、React中的组件
react组件就是自己定义的非html标签,规定react组件首字母大写:
class App extends Component{}
登录后复制
二、父子组件
组件的相互调用中,把调用者称为父组件,被调用者称为子组件:
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) }}export default Children;
{this.props.msg}
登录后复制
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;
登录后复制
四、子组件给父组件传值
子组件向父组件传值通过触发方法来传值
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;```
登录后复制
【相关推荐:Redis视频教程】
以上就是react中什么是父子组件的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2927991.html