博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
React基础——组件状态state、属性props
阅读量:4131 次
发布时间:2019-05-25

本文共 3810 字,大约阅读时间需要 12 分钟。

import React from 'react';  // 此句不能少import ReactDom from 'react-dom';class Taggle extends React.Component{    state = {        flag: true    }    handleClick(e){        e.preventDefault()  // 阻止默认行为,要放在开头        console.log(e);        console.log(e.target);  // target为html标签        // this.state.flag = !this.state.flag;  // 修改state状态        this.setState({flag: !this.state.flag})  // 官方推荐这样修改state    }    render(){        return 
{/*必须bind,不然this没有定义*/} {this.state.flag.toString()} {/*注意必须转换成字符串*/}
}}class Sub extends React.Component{ // 子组件 render(){ // return sub element ==== {'2>1?true:false'} // true和false打印不出来,必须改为字符串 return sub element ==== {true.toString()} }} // class component,类名必须是大驼峰,所有标签必须封口,单标签也得封口,所有标签都是小写class App extends React.Component{ // First就是组件,就是元素,组件名第一个字母必须大写开头 state = { // state是一个对象,state的变化会影响render函数的执行 p1: 'Curry', p2: '.Klay' } // render组件的渲染函数 render(){ console.log('~~~~~~~~~~~~~') //this.state.p1 = 'Warriors' // 修改了p1的内容,但是不建议这样修改 // this.setState({p1: 'Warriors'}) // 放在这里会递归 setTimeout(() => this.setState({p1: 'Warriors'}), 3000) // 注意这里还是有递归的,只是后来虚拟DOM没有改变,浏览器没有重新渲染 // return
App Component
; // 只能有一个顶级元素 return
{this.state.p1}{this.state.p2}

}}class Second extends React.Component{ // First就是组件,就是元素 // render组件的渲染函数 render(){ return React.createElement('div', null, 'test string'); }}

 

import React from 'react'import ReactDom from 'react-dom'class Toggle extends React.Component{    constructor(props){        super(props)        this.state = {            flag: true,            count: 0        }    }    // state = {    //     flag: true,    //     count: 0    // }    handleClick(e){        // console.log(e)        // console.log(e.target)  // target为id=t1的div标签        // console.log(this)        // // this.setState({flag: !this.state.flag})        this.setState({count: this.state.count + 1})    }        // handleOver(e){    //     this.setState({count: this.state.count + 1})    // }    render(){        console.log('render taggle')        return 
{/* 一定要用bind绑定this,否则直接结果是undefined,参考之前篇幅的this的坑 */} {/* style标签里第一大括号是表达式,第二个大括号是对象 */} {this.state.flag.toString()}---{this.state.count}
{this.props.name}{this.props.parent.state.myname}
{this.props.children} {/* 可以拿到Toggle的所有子标签的内容 */}
}}// props属性在内部是只读属性,不能修改,要想修改,需要从外部注入class App extends React.Component{ constructor(props){ super(props) // 必须调用super父类构造器,否则报错 this. state = { // 类中定义state // p1: 'Curry', // p2: '.Klay', childName: 'Curry', myname: '.warriors', count: 0 } } // state = { // // p1: 'Curry', // // p2: '.Klay', // childName: 'Curry', // myname: '.warriors', // count: 0 // } handleClick(e){ // this.setState({childName: this.state.childName + '1'}) this.setState({count: this.state.count + 1}) } render(){ console.log('APP render') setTimeout(() => this.setState({p1: 'Warriors'}), 3000); // 每隔3秒 return
{/* {this.state.p1}{this.state.p2} */} {/* child's name = {this.state.childName} */} count = {this.state.count}

{/* 自定义了两个属性通过props传给Toggle组件对象 */}
金州拉文
勇士总冠军(我是锚点) {/*a和span标签是Toggle的子元素 ,子元素通过props.children访问*/}
}}ReactDom.render(
, document.getElementById('root'))

转载地址:http://avfvi.baihongyu.com/

你可能感兴趣的文章
Unifrax宣布新建SiFAB™生产线
查看>>
艾默生纪念谷轮™在空调和制冷领域的百年创新成就
查看>>
NEXO代币持有者获得20,428,359.89美元股息
查看>>
Piper Sandler为EverArc收购Perimeter Solutions提供咨询服务
查看>>
RMRK筹集600万美元,用于在Polkadot上建立先进的NFT系统标准
查看>>
JavaSE_day14 集合中的Map集合_键值映射关系
查看>>
异常 Java学习Day_15
查看>>
Mysql初始化的命令
查看>>
MySQL关键字的些许问题
查看>>
浅谈HTML
查看>>
css基础
查看>>
Servlet进阶和JSP基础
查看>>
servlet中的cookie和session
查看>>
过滤器及JSP九大隐式对象
查看>>
软件(项目)的分层
查看>>
菜单树
查看>>
Servlet的生命周期
查看>>
JAVA八大经典书籍,你看过几本?
查看>>
《读书笔记》—–书单推荐
查看>>
JAVA数据类型
查看>>