react基本知识

react的render函数

很多时候我们都认为react就是render大法,也确实是,每次我们都是通过render来重新渲染页面,我们来看render的例子:

render() {
    //JSX 语法
    // {}
    let helloWorld = 'yuxi';
    // array
    return (
      <span>
        <div className="App">
             helloWorld,{helloWorld};
        </div>
       
        <div>
          <States disable={false}
            handShow={this.handShow}>
            <span>hello world</span>
            <span>hello yuxi</span>
            <span>hello xiaoyu</span>
        </States>
        </div>
      </span>
    );
  }

需要注意的是:render应该只做页面展示和渲染,不应该有构造函数,回调函数,声明周期等。

react的state状态

statereact的状态管理,react是单向数据流的,当我们需要对数据做改变的时候应该是:

constructor() {
    super();
    console.log('this is constructor....')
    this.state = {
      disable:false,
    }
  }
  handClick = () => this.setState(
    {
      disable: !this.state.disable
    }
  )
react的props属性

当我们需要把父组件的数据或者函数传递给子组件的时候,我们一般都使用props,我们来看一个例子:
传递给子组件:

     <States disable={false}
            handShow={this.handShow}>
            <span>hello world</span>
            <span>hello yuxi</span>
            <span>hello xiaoyu</span>
        </States>

子组件获取:

 let args = this.props.disable;

我们再来比较一下reactprops,到底什么时候用哪个呢?
简单来理解:

  • 如果需要改变数据就使用:state
  • 如果需要传递数据就使用:props
    有一篇文章写得比较好:ReactJS: Props vs. State
react的声明周期

react常用的声明周期有:

  • constructor 构造函数
  • componentWillMount
  • componentDidMount
  • componentWillUpdate
  • componentDidUpdate
  • componentWillUnmount
    总的来说我们常用的就是:
    constructor,componentWillMount,componentDidMount
    来看一个简单例子:
import React, { Component } from 'react'

export class States extends Component {
  constructor() {
    super();
    console.log('this is constructor....')
    this.state = {
      disable:false,
    }
  }
  handClick = () => this.setState(
    {
      disable: !this.state.disable
    }
  )

  componentWillMount() {
  
    console.log('this is will mount')
  }

  componentDidMount() {
    console.log('this is did mount');
  }

  componentWillUpdate() {
    console.log('this is will update');
  }

  componentDidUpdate() {
    console.log('this is did update');
  }

 

  handShow = () => {
    let args = this.state.disable;
    this.props.handShow(args);
  }

  handFocus = () => {
    this.refs.myFocus.focus();
  }

  render() {
    // state practise 
    // set this.props.children
    // operate virtral DOM
    let args = this.props.children;
    let DOME = React.Children.map(args,(child) => {
        return <li>{child}</li>
    });
    console.log(DOME)
    return (
      <div>
       <input type="text" disabled={this.state.disable}></input>
       <button onClick={this.handClick}> click me</button>
       <button onClick={this.handShow}>show</button>
       <ol>
         {DOME}
       </ol>
       <input type="text" ref='myFocus'></input>
       <button onClick={this.handFocus}> focus</button>
      </div>
    )
  }
}

export default States

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 作为一个合格的开发者,不要只满足于编写了可以运行的代码。而要了解代码背后的工作原理;不要只满足于自己的程序...
    六个周阅读 8,508评论 1 33
  • 40、React 什么是React?React 是一个用于构建用户界面的框架(采用的是MVC模式):集中处理VIE...
    萌妹撒阅读 1,040评论 0 1
  • 本笔记基于React官方文档,当前React版本号为15.4.0。 1. 安装 1.1 尝试 开始之前可以先去co...
    Awey阅读 7,798评论 14 128
  • HTML模版 之后出现的React代码嵌套入模版中。 1. Hello world 这段代码将一个一级标题插入到指...
    ryanho84阅读 6,302评论 0 9
  • 16岁的时候,他梳莫西干头,喜欢看古惑仔,并像大哥陈浩南一样,在学校收了一群小弟,很是有几分威望。一个初冬的下午,...
    卢四毛阅读 1,371评论 5 25