美文网首页
react 父子组件传值

react 父子组件传值

作者: 少__年 | 来源:发表于2019-03-15 20:27 被阅读0次

//父组件

import React, {Component} from 'react';

import {Todo} from './index.js'

import Zitodo from './zitodo'

class TodoList extends Component {

    constructor(props) {

        super(props);

        this.state = {

            arr: [true, false, false]

}

}

    handleChange = (i) => {

        const arr = this.state.arr.map((item, index) => {

            if (index === i) return true;

            return false

        });

        this.setState({

            arr

        });

      // console.log(arr)

    };

    render() {

        const data = Todo;

        // console.log(data)

        return (

            <div>

{

                    //将对象返回成数组

                    Object.keys(data).map((item, index) => {

                        return (
                          {/* 子组件将父组件需要的内容传入*/}
                            <Zitodo

                                key={index}

                                {...data[item]}

                                show={this.state.arr[index]}

                                change={this.handleChange}

                                index={index}

/>

)

})

}

            </div>

)

}

}

export default TodoList;


//子组件

import React, {Component} from 'react';

class Zitodo extends Component {

    constructor(props) {

        super(props);

        //console.log(props); //查看父组件传过来的内容是否成功

        this.state = {

            onOff: this.props.show

        }

}

    shouldComponentUpdate(nextProps, nextState) {
          
        if (this.state.onOff !== nextProps.show) {

            this.setState({

                onOff: nextProps.show

            })

}

        return true

    }

    handleClick = () => {

        const onOff = !this.state.onOff;

        this.props.change(this.props.index);

        this.setState({

            onOff

        });

        //console.log(onOff)

    };

    render() {

        const {floor, random} = Math;

        const color = (a, b) => {

            return floor(random() * (b - a + 1) + a)

};

        const {name, list} = this.props;

        return (

            <div style={{width: '200px'}}>

                <h2 style={{background: 'green'}}

                    onClick={this.handleClick}

>

                    {name}</h2>

                {/* 返回对象属性的name值*/}

{

                    this.state.onOff && (

                        <ol style={{background: 'pink'}}>

{

                                list && list.map((item, index) => {

                                    return (

                                        <li style={{background: `rgba(${color(0, 255)},${color(0, 255)},${color(0, 255)},.5)`}}

                                            key={index}>

                                            <p>{item.id}</p>

                                            <p>{item.name}</p>

                                            <p>{item.age}</p>

                                        </li>

)

})

}

                        </ol>

)

}

            </div>

);

}

}

export default Zitodo;

此内容仅供本人做笔记使用

相关文章

  • react-父子组件间通信

    React-父子组件间通信 父组件传 值 给子组件, 传 方法 给子组件 子组件接收 值 ,触发父组件的 方法

  • Vue父子组件通信和双向绑定

    本篇文章主要介绍父子组件传值,组件的数据双向绑定。 1. 基础父子组件传值 父子组件传值,这是Vue组件传值最常见...

  • 学习react的第二天(1)

    react组件传值 为了防止数据乱流,react规定,不借助外力,只支持父子传值 父 -> 子 1. 在父元素中,...

  • react 父子组件传值(兄弟传值)

    react中父子组件传值 父向子: 父组件通过自定义属性向子组件传值,子组件通过this.props.属性名接收子...

  • React入门(二)组件

    本节知识点 (1)React组件化 (2)React父子组件传值 (一)组件 在React中组件都是对应的一个个类...

  • 组件之间的传值

    组件之间的传值,包括父子组件传值,兄弟组件之间的传值,其中父子组件包括父组件向子组件传值和子组件向父组件传值,现在...

  • vue 父子组件传值 $on sync v-model

    1、正常的父子组件传值2、使用sync 实现父子组件传值3、使用v-model实现父子组件传值

  • React父子组件间的传值的方法

    在单页面里面,父子组件传值是比较常见的,这篇文章主要介绍了React父子组件间的传值的方法,小编觉得挺不错的,现在...

  • (VUE3) 四、组件传值(父子组件传值 & 祖孙组件传值 &v

    1.父子组件传值 vue2中的父子组件传值:父组件: 子组件: vue3中的父子组件传值: 还是用props接收父...

  • vue、react对比

    一、父子通信 1. 父组件通过props向子组件传值 vue:父组件 子组件接收 react:父组件: 子组件: ...

网友评论

      本文标题:react 父子组件传值

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