react实战分析:评论功能(二)


  • 0

    父组件向子组件传递回调函数,子组件props接收到的是个数组,props下面没有onSubmit这个函数评论功能(二)

    props:
    children: Array(2)
    0: "onSubmit = "
    1: ƒ (msg)
    length: 2
    
    //父组件
      handleSubmitComment(a) {
        // console.log(a)
        this.state.lists.push(a)
        this.setState({
          lists: this.state.lists
        })
      }
    
       <ComInput>
            onSubmit = {this.handleSubmitComment.bind(this)}
       </ComInput>
    
    //子组件
        handleSubmit(){
            console.log(this)
            if(this.props.children[1]){
                const {username,content} = this.state
                this.props.children[1]({username,content})
            }
            this.setState({
                content:'',
                username:''
            })
        }
    

登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待