美文网首页前端框架
React 子组件向父组件传递

React 子组件向父组件传递

作者: coderhzc | 来源:发表于2021-12-21 16:44 被阅读0次

子组件向父组件传值

需求:当父组件中的counter 需要使用子组件中的点击事件累加

父组件 index.js 页面

import React, { Component } from "react";
import ChildButton from "./child";
export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      counter: 0,
    };
  }
  render() {
    const { counter } = this.state;
    return (
      <div>
        <h2>当前计数:{counter}</h2>
        <button onClick={()=>this.btnClick()}>父组件 + 1</button>
        <hr/>
        <ChildButton changeBtnClick={()=>this.btnClick()}/>
      </div>
    );
  }
  //父组件事件
  btnClick() {
    this.setState({
      counter: this.state.counter + 1
    })
  }
}

子组件 child.js页面

import React, { Component } from 'react'

export default class Child extends Component {
  constructor(props) {
    super(props)
  }
  render() {
    return (
      <div>
        <button onClick={()=> this.btn()}>子组件 + 1</button>
      </div>
    )
  }
  btn() {
    const {changeBtnClick} = this.props
    changeBtnClick()
  }
}

实际截图

image.png

综合案例:

App.js

import React, { Component } from "react";
import TabControl from "./TabControl";

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      headerTitle: ["流行", "精选", "新款"],

      currentTitle: "流行",

      timeZoneIndex: 0,
    };
  }
  render() {
    const { headerTitle, currentTitle, listTable } = this.state;
    return (
      <div>
        <TabControl
          titles={headerTitle}
          itemChange={(index) => this.itemChange(index)}
        />
        <h1>{currentTitle}</h1>

      </div>
    );
  }
  itemChange(index) {
    this.setState(
      {
        currentTitle: this.state.headerTitle[index],
        timeZoneIndex: index,
      });
  }
}

子页面TabControl.js

import React, { Component } from "react";
import "./style.css";

export default class TabControl extends Component {
  constructor(props) {
    super(props);
    this.state = {
      currentIndex: 0,
    };
  }
  render() {
    const { titles } = this.props;
    const { currentIndex } = this.state;
    return (
      <div>
        <div className="content-box">
          {titles.map((item, index) => {
            return (
              <div
                key={index}
                className={
                  "content-box-item " + (currentIndex === index ? "active" : "")
                }
                onClick={() => this.itemClick(index)}
              >
                <span>{item}</span>
              </div>
            );
          })}
        </div>
      </div>
    );
  }

  itemClick(index) {
    const { itemChange } = this.props;

    this.setState({
      currentIndex: index,
    });
    itemChange(index);
  }
}

css页面 style.css

.content-box {
  height: 45px;
  line-height: 45px;
  display: flex;
}
.content-box-item {
  flex: 1;
  text-align: center;
}

.content-box-item span {
  padding: 5px 8px;
}
.content-box-item.active {
  color: deeppink;
}

.content-box-item.active span {
  border-bottom: 3px solid deeppink;
}

实际截图

.content-box {
  height: 45px;
  line-height: 45px;
  display: flex;
}
.content-box-item {
  flex: 1;
  text-align: center;
}

.content-box-item span {
  padding: 5px 8px;
}
.content-box-item.active {
  color: deeppink;
}

.content-box-item.active span {
  border-bottom: 3px solid deeppink;
}

相关文章

  • 2020-06-16 React组件通信

    在使用react的,不可避免的组件间的消息传递 1:父组件向子组件传值(通常的是父组件向子组件传递props,子组...

  • react父子组件之间的通信

    1、父组件向子组件传递 父组件向子组件传递比较简单,如下: 父组件结构: 子组件结构: 子组件接收: 2、子组件向...

  • ReactNative组件间的通信

    父组件向子组件通信 父组件向子组件传值 父组件向子组件传递方法 子组件向父组件通信 子组件向父组件传值 子组件向父...

  • 总结

    React组件之间的通信方式 1.父组件向子组件通信 React数据流动是单向的,通过props传递 2.子组件向...

  • 19.ReactNative组件间的通信

    父组件向子组件通信: 父组件向子组件传值 通过props传递 在父组件中name='我是父组件向子组件传递的参数'...

  • ReactNative组件间的通信

    父组件向子组件通信 父组件向子组件传值 通过props传递 在父组件中name='我是父组件向子组件传递的参数' ...

  • React父子组件间的通信

    React数据流动是单向的,父组件向子组件的通信也是最常见的方式。父组件通过props向子组件传递需要的信息。子组...

  • wx小程序-03 父、子组件传值

    父组件向子组件传值: 父组件: 子组件:如果父组件不传递text,则会使用默认的text值。 子组件向父组件传递数...

  • 父子组件之间的信息通信

    1.子组件向父组件传递数据 ①向子组件传递number属性,并把父组件传给子组件的number渲染到子组件中。父组...

  • 弹窗设置(父子传参原理)

    父组件: 1.父组件向子组件传递数据父组件绑定属性,给子组件传递数据子组件通过props接收父组件传递过来的数据子...

网友评论

    本文标题:React 子组件向父组件传递

    本文链接:https://www.haomeiwen.com/subject/tvcxqrtx.html