react深入浅出-demo1

index.js

import React, { Component } from 'react'
import CommentInput from './commentInput'
import CommentList from './commentList'


export default class Index extends Component {
    constructor(){
        super()
        this.state={
            comments:[]
        }
    }

    handleSubmitComment(comment){
        this.state.comments.push(comment)
        this.setState({
            comments: this.state.comments
        })
        console.log(comment)
    }
   
    render() {
        return (
            
) } }

commentInput.js

import React, { Component } from 'react'

export default class CommentInput extends Component {
    constructor(){
        super()
        this.state={
            username:'',
            content:'',
        }
    }

    usernameChange(e){
        this.setState({
            username:e.target.value
        })
    }

    contentChange(e){
        this.setState({
            content:e.target.value
        })
    }

    handleSubmit(){
        if(this.props.onSubmit){
            const {username,content}=this.state;
            this.props.onSubmit({username,content})
        }
        this.setState({ 
            username:'',
            content:''
        })
    }

    render() {
        return (
            
用户名:
评论: