zoukankan      html  css  js  c++  java
  • react 父子组件通信

    1.父组件向子组件传值

    父组件

    import React from "react"
    import ChildrenList from "./ChildrenList "
    
    class Comment extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
              arr: [{
                    "userName": "fangMing", "text": "方明", "result": true
                },{
                    "userName": "liSi", "text": "黎明", "result": true
                },]
             }
       }
    
    render() {
          return (
            <div>
               <ChildrenList arr={this.state.arr} />  //这里把state里面的arr传递到子组件
            </div>
          )
    }
    
    
    
    }
    
    
    export default Comment;
    

    子组件

    import React from "react"
    
    class ChildrenList extends React.Component {
        constructor(props) {
            super(props);
       }
    
    render() {
          return (
          <div className="list">
                    <ul>
                        {
                            this.props.arr.map(item => { //这个地方通过this.props.arr接收到父组件传过来的arr,然后在{}里面进行js的循环
                                return (
                                    <li key={item.userName}>
                                        {item.userName} 评论是:{item.text}
                                    </li>
                                )
                            })
                        }
                    </ul>
                 
                </div>
          )
    }
    
    
    
    }
    
    
    export default ChildrenList ;
    

    2.子组件向父组件传值

    父组件

    import React from "react"
    import ChildrenList from "./ChildrenList "
     
    class Comment extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                parentText: "this is parent text",   
                 arr: [{
                    "userName": "fangMing", "text": "方明", "result": true
                },{
                    "userName": "liSi", "text": "黎明", "result": true
                },]
            }
        }
     
        fn(data) {
            this.setState({
                parentText: data //把父组件中的parentText替换为子组件传递的值
            },() =>{
               console.log(this.state.parentText);//setState是异步操作,但是我们可以在它的回调函数里面进行操作
            });
     
        }
     
     
     
        render() {
            return (
                <div>
                    //通过绑定事件进行值的运算,这个地方一定要记得.bind(this),
                不然会报错,切记切记,因为通过事件传递的时候this的指向已经改变
                    <ChildrenList arr={this.state.arr} pfn={this.fn.bind(this)} />
                    <p>
                        text is {this.state.parentText}
                    </p>
            
                </div>
     
            )
        }
    }
     
    export default Comment; 
    

    子组件

    import React from "react"
     
    class ChildrenList extends React.Component {
        constructor(props) {
            super(props);
            this.state = ({
                childText: "this is child text"
            })
     
        }
        clickFun(text) {
            this.props.pfn(text)//这个地方把值传递给了props的事件当中
        }
        render() {
            return (
                <div className="list">
                    <ul>
                        {
                            this.props.arr.map(item => {
                                return (
                                    <li key={item.userName}>
                                        {item.userName} 评论是:{item.text}
                                    </li>
                                )
                            })
                        }
                    </ul>
                    //通过事件进行传值,如果想得到event,可以在参数最后加一个event,
                    这个地方还是要强调,this,this,this
                    <button onClick={this.clickFun.bind(this, this.state.childText)}>
                        click me
                    </button>
                </div>
            )
        }
    }
     
    export default ChildrenList ;  
    
  • 相关阅读:
    PHP openssl_encrypt函数安全漏洞
    WordPress Organizer插件安全漏洞
    WordPress Poll插件多个SQL注入和安全绕过漏洞
    Redis 重写任意文件漏洞
    WordPress Events Manager插件多个跨站脚本漏洞
    项目建议书编写总结
    www.beihua.edu.cn计划摘录
    《易经》对中华文化的影响
    网站调研资料记录
    积分和排名
  • 原文地址:https://www.cnblogs.com/IT123/p/10881158.html
Copyright © 2011-2022 走看看