摘要:新搭建的個人博客,本文地址學(xué)習(xí)筆記用重寫在一開始的時候配置中我們就加入了的支持,就是下面的配置,但之前的學(xué)習(xí)筆記都使用的完成,所以專門作一篇筆記,記錄使用完成創(chuàng)建相關(guān)文件修改,增加該入口文件修改,引入該文件做個簡單的測試,看下瀏覽器全部用來
新搭建的個人博客,本文地址:React學(xué)習(xí)筆記3:用es2015(ES6)重寫CommentBox
在一開始的時候webpack配置中我們就加入了es2015的支持,就是下面的配置,但之前的學(xué)習(xí)筆記都使用的es5完成,所以專門作一篇筆記,記錄使用es6完成CommentBox
query: { presets: ["es2015","react"] }
1、創(chuàng)建相關(guān)文件
touch src/commentEs6.js //修改webpack,增加該入口文件 entry:{ "index":"./src/index.js", "comment":"./src/comment.js", "commentEs6":"./src/commentEs6.js", }, //修改build/index.html,引入該文件
2、做個簡單的測試,看下瀏覽器
//require全部用import來完成 import React from "react"; import ReactDom from "react-dom"; //es6支持類,通過類繼承完成組件的開發(fā) class CommentBox extends React.Component { render () { returnHello World} } //渲染 ReactDom.render(,document.getElementById("content") )
3、直接貼出完整修改后的代碼
import React from "react"; import ReactDom from "react-dom"; import marked from "marked" import $ from "jquery" class CommentBox extends React.Component { constructor(props){ super(props) //state 初始化直接在構(gòu)造函數(shù)完成 this.state = {data:[]} } //函數(shù)的書寫方式也發(fā)生了變化 loadCommentsFromServer(){ //還不太明白,總之this到了ajax中指向的對象就發(fā)生了變化 var url = this.props.url $.ajax({ url:url, dataType:"json", cache:false, //利用箭頭函數(shù)傳參,可以少去了bind this success:(data) => { this.setState({data: data}) }, error:(xhr,status,err) => { console.log(url,status,err.toString()) } }) } handleSubmitComment(data){ var url = this.props.url $.ajax({ url:url, type:"POST", data:data, dataType:"json", cache:false, success:(data) => { this.setState({data:this.state.data.concat(data)}); }, error:(xhr,status,err) => { console.log(this.props.url,status,err.toString()) } }) } componentDidMount(){ this.loadCommentsFromServer() //setTimeout(this.loadCommentsFromServer,2000) } render() { return () } } ReactDom.render() } } class CommentList extends React.Component{ constructor(props) { super(props) } render(){ var commentNodes = this.props.data.map(function(comment){ return (Comments
{comment.text} ) }); return ({commentNodes}) } } class Comment extends React.Component { constructor(props) { super(props) } rawMarkup(){ var rawMarkup = marked(this.props.children.toString(),{sanitize:true}) return {__html:rawMarkup} } render(){ return () } } class CommentForm extends React.Component{ constructor(props) { super(props) this.state = {author:"",text:""} } handleAuthorChange(event){ this.setState({author:event.target.value}) } handleTextChange(event){ this.setState({text:event.target.value}) } handleSubmit(event){ event.preventDefault(); var author = this.state.author.trim() var text = this.state.text.trim() if(!text||!author) { return; } this.props.onSubmitComment({author:author,text:text}); this.setState({author:"",text:""}) return false; } render(){ return ({this.props.author}
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/78580.html
摘要:瀏覽器看效果效果顯示效果就是瞎比比我不聽瞎比比解析后的文本直接被顯示在頁面上,并沒有被瀏覽器解析,這是為了防止被攻擊而作的保護(hù)措施。 新搭建的個人博客,本文地址:React學(xué)習(xí)筆記2:React官方CommentBox實(shí)踐所有的操作是繼續(xù)上一個學(xué)習(xí)筆記,參考的是React官方的CommentBox,不過不是100%按照其實(shí)現(xiàn)。參考:https://facebook.github.io/...
摘要:組件通信實(shí)現(xiàn)表單提交昨晚做了一個的例子,主要實(shí)現(xiàn)的是提交表單實(shí)現(xiàn)評論的功能,在做之前先簡單介紹一下。并稱為前端大框架,就目前來看,盡管發(fā)布了也在今年月份發(fā)布了,更不在話下,大家要是想學(xué)習(xí)的話可以去官網(wǎng)學(xué)習(xí)。 react組件通信實(shí)現(xiàn)表單提交 昨晚做了一個react的例子,主要實(shí)現(xiàn)的是提交表單實(shí)現(xiàn)評論的功能,在做之前先簡單介紹一下React。 showImg(https://segment...
摘要:例子全解析近些時間一直在關(guān)注,關(guān)于如何學(xué)習(xí)可以參照鏈接的文章自行制定計(jì)劃。千里之行,始于足下。此外,輸出的內(nèi)容要解析為,而在默認(rèn)情況下,基于預(yù)防攻擊的考慮,對輸出的內(nèi)容是不解析為的。 React-tutorial例子全解析 Talk is cheap,Show me the code 近些時間一直在關(guān)注React,關(guān)于如何學(xué)習(xí)React可以參照鏈接的文章自行制定計(jì)劃。千里之行,始于足下...
摘要:新搭建的個人博客,本文地址學(xué)習(xí)筆記環(huán)境搭建本文的書寫環(huán)境為,之后會補(bǔ)充下的差異創(chuàng)建學(xué)習(xí)目錄初始化項(xiàng)目根據(jù)相關(guān)提示完善信息,入口文件安裝相關(guān)包,并且使用也就是支持,需要包,因?yàn)槲抑白鰝€一些相關(guān)項(xiàng)目,所以部分包已經(jīng)全局安裝,比如等等,大家 新搭建的個人博客,本文地址:React學(xué)習(xí)筆記1:環(huán)境搭建 本文的書寫環(huán)境為mac,之后會補(bǔ)充windows下的差異 1、創(chuàng)建學(xué)習(xí)目錄 mkdir l...
閱讀 2768·2021-11-22 13:54
閱讀 2707·2021-10-14 09:42
閱讀 4057·2021-09-28 09:47
閱讀 2175·2021-09-03 10:28
閱讀 1220·2021-07-26 23:38
閱讀 2570·2019-08-30 15:54
閱讀 2652·2019-08-29 16:35
閱讀 1438·2019-08-29 15:42