首页 > 编程语言 >第一个react.js程序:create and show comment

第一个react.js程序:create and show comment

时间:2023-05-27 11:22:50浏览次数:56  
标签:comment React show create content user props createElement id

import React, { Component } from "react";
import { render } from "react-dom";
import PropTypes from "prop-types";

const node = document.getElementById("root");

const data = {
  post: {
    id: 123,
    content:
      "What we hope ever to do with ease, we must first learn to do with diligence. — Samuel Johnson",
    user: "Mark Thomas"
  },
  comments: [
    {
      id: 0,
      user: "David",
      content: "such. win."
    },
    {
      id: 1,
      user: "Haley",
      content: "Love it."
    },
    {
      id: 2,
      user: "Peter",
      content: "Who was Samuel Johnson?"
    },
    {
      id: 3,
      user: "Mitchell",
      content: "@Peter get off Letters and do your homework"
    },
    {
      id: 4,
      user: "Peter",
      content: "@mitchell ok :P"
    }
  ]
};

class Post extends Component {
  constructor(props) {
    super(props);
  }
  render() {
    return React.createElement(
      "div",
      {
        className: "post"
      },
      React.createElement(
        "h2",
        {
          className: "postAuthor",
          id: this.props.id
        },
        this.props.user,
        React.createElement(
          "span",
          {
            className: "postBody"
          },
          this.props.content
        ),
        this.props.children
      )
    );
  }
}

Post.propTypes = {
  user: PropTypes.string.isRequired,
  content: PropTypes.string.isRequired,
  id: PropTypes.number.isRequired
};

class Comment extends Component {
  constructor(props) {
    super(props);
  }
  render() {
    return React.createElement(
      "div",
      {
        className: "comment"
      },
      React.createElement(
        "h2",
        {
          className: "commentAuthor"
        },
        this.props.user,
        React.createElement(
          "span",
          {
            className: "commentContent"
          },
          this.props.content
        )
      )
    );
  }
}

Comment.propTypes = {
  id: PropTypes.number.isRequired,
  content: PropTypes.string.isRequired,
  user: PropTypes.string.isRequired
};

class CreateComment extends Component {
  constructor(props) {
    super(props);
    this.state = {
      content: "",
      user: ""
    };
    this.handleUserChange = this.handleUserChange.bind(this);
    this.handleTextChange = this.handleTextChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }
  handleUserChange(event) {
    const val = event.target.value;
    this.setState(() => ({
      user: val
    }));
  }
  handleTextChange(event) {
    const val = event.target.value;
    this.setState({
      content: val
    });
  }
  handleSubmit(event) {
    event.preventDefault();
    this.props.onCommentSubmit({
      user: this.state.user.trim(),
      content: this.state.content.trim()
    });

    this.setState(() => ({
      user: "",
      content: ""
    }));
  }
  render() {
    return React.createElement(
      "form",
      {
        className: "createComment",
        onSubmit: this.handleSubmit
      },
      React.createElement("input", {
        type: "text",
        placeholder: "Your name",
        value: this.state.user,
        onChange: this.handleUserChange
      }),
      React.createElement("input", {
        type: "text",
        placeholder: "Thoughts?",
        value: this.state.content,
        onChange: this.handleTextChange
      }),
      React.createElement("input", {
        type: "submit",
        value: "Post"
      })
    );
  }
}
CreateComment.propTypes = {
  onCommentSubmit: PropTypes.func.isRequired,
  content: PropTypes.string
};

class CommentBox extends Component {
  constructor(props) {
    super(props);
    this.state = {
      comments: this.props.comments
    };
    this.handleCommentSubmit = this.handleCommentSubmit.bind(this);
  }
  handleCommentSubmit(comment) {
    const comments = this.state.comments;
    comment.id = Date.now();
    const newComments = comments.concat([comment]);
    this.setState({
      comments: newComments
    });
  }
  render() {
    return React.createElement(
      "div",
      {
        className: "commentBox"
      },
      React.createElement(Post, {
        id: this.props.post.id,
        content: this.props.post.content,
        user: this.props.post.user
      }),
      this.state.comments.map(function(comment) {
        return React.createElement(Comment, {
          key: comment.id,
          id: comment.id,
          content: comment.content,
          user: comment.user
        });
      }),
      React.createElement(CreateComment, {
        onCommentSubmit: this.handleCommentSubmit
      })
    );
  }
}

CommentBox.propTypes = {
  post: PropTypes.object,
  comments: PropTypes.arrayOf(PropTypes.object)
};

render(
  React.createElement(CommentBox, {
    comments: data.comments,
    post: data.post
  }),
  node
);

 

标签:comment,React,show,create,content,user,props,createElement,id
From: https://www.cnblogs.com/saaspeter/p/17436445.html

相关文章

  • webstore报 ESLint: Expected space or tab after '//' in comment.(spaced-comment)
    webstore报:ESLint:Expectedspaceortabafter'//'incomment.(spaced-comment) 解决方法:禁用ESLINT......
  • Cannot run program “G:\Java\bin\java.exe“ (in directory “C:\compile-serve
    今夜拾起两年前早已遗忘的java踌躇满志打开idea发现早已物是人非他报错了,然鹅并没有解决,解决方法我把Structure里多余的jdk删掉,只剩下jdk1.8,如图,就解决了......
  • ctfshow终极考核
    信息收集这个环境就只涉及目录扫描了[18:04:02]200-43B-/.bowerrc[18:04:03]200-34B-/.gitignore[18:04:04]200-2KB-/.travis.yml[18:04:24]200-3KB-/page.php[18:04:28]200-19B-/robots.txt/robots.txt得到source.txt访问发......
  • this.$refs.ref 不存在 undefined v-if和v-show 的区别
    <divv-if='true'>  <div>....<el-inputref='ref'/></div></div> 上面的代码,引用this.$refs.ref=undefined; 改为v-show解决。 网上有好多说法是:this.$nextTick(()=>{放这里});在这里不能解决的。原因:v-if条件不满足,document中是不存在的,v......
  • useRef 与 createRef 的区别
    两者区别:createRef每次渲染都会返回一个新的引用,而useRef每次都会返回相同的引用。useRef一般用于函数组件useRef不仅仅是用来管理DOMref的,它还相当于this,可以存放任何变量.当useRef的内容发生变化时,它不会通知您。更改.current属性不会导致组件重新渲染。因为......
  • django update_or_create
    update_or_create是Django中的一个方法,用于更新或创建数据库记录。它的作用是,如果数据库中存在符合指定条件的记录,则更新该记录的字段值;如果不存在符合条件的记录,则创建新的记录。使用update_or_create方法需要指定两个参数:defaults:一个字典,用于指定要更新或创建的字段及其对应......
  • grant create synonym to SCOTT
    ConnectedtoOracleDatabase11gEnterpriseEditionRelease11.2.0.1.0ConnectedasSYSTEM@ORCLSQL>grantcreatesynonymtoSCOTT;GrantsucceededSQL>connscott/tiger;ConnectedtoOracleDatabase11gEnterpriseEditionRelease11.2.0.1.0......
  • react-create-app 创建的项目添加保存自动格式化
    前言首先,react-create-app创建的项目是自带安装eslint的,所以我们要安装prettier及相关插件参考=>https://juejin.cn/post/6844903901544742925安装的包这些都是开发时依赖,注意安装位置eslint-config-react-app打开modules文件,可以看到是已经预安装的,无需重复......
  • ctfshow php特性
    web111源代码highlight_file(__FILE__);error_reporting(0);include("flag.php");functiongetFlag(&$v1,&$v2){eval("$$v1=&$$v2;");//这里是一个赋值语句把v2的值复制下面通过get获得的$$v1值var_dump($$v1);//打印$$v1的值}if(isset($_......
  • 实现 React 简易版 createElement 和 render 方法
    前言在React中,我们都知道可以写jsx代码会被编译成真正的DOM插入到要显示的页面上。这具体是怎么实现的,今天我们就自己动手做一下。实现createElement方法这个方法平时开发我们并不会用到,因为它是经babel编译后的代码,我们新建一个React项目,index.js最简单的代码结构如......