React 笔记

JSX

  1. React 采用jsx语法,需用bable.js转义.
  2. JSX 语法可以和html 混写. html以< 开头, JSX以( 开头
  3. 采用ReactDOM.render 来定义React渲染
  4. 数组 {array} 在render方法会被concat(拼接)
  5. React.creatClass(new function() {}); 来创建一个新的React Component
var lol = React.createClass(
          render: function() {
                return <h1>Hello world! {this.props.name}</h1>
          });
ReactDOM.render(
  <HelloMessage name="jason" />,
  document.getElementById('example')
);
  1. Component 的取名首字母必须大写
  2. 通过 React.Children来访问子节点:
<script type="text/babel">
      var NotesList = React.createClass({
        render: function() {
          return (
            <ol>
              {
                React.Children.map(this.props.children, function (child) {
                  return <li>{child}</li>;
                })
              }
            </ol>
          );
        }
      });
  1. this.propTypes 定义组件接受的参数
var MyTitle = React.createClass({
  propTypes: {
    title: React.PropTypes.string.isRequired,
  },

  render: function() {
     return <h1> {this.props.title} </h1>;
   }
});
  1. this.refs.{DOM} 来引用render的dom
var MyComponent = React.createClass({
  handleClick: function() {
    this.refs.myTextInput.focus();
  },
  render: function() {
    return (
      <div>
        <input type="text" ref="myTextInput" />
        <input type="button" value="Focus the text input" onClick={this.handleClick} />
      </div>
    );
  }
});
  1. getInitialState 在invoked before a component is mounted

  2. use component attributes to register event handlers, just like
    *onClick *onKeyDown onCopy
    etc. Official Document has all supported events

  3. this.state 描述变化量,可通过Ui操作更新.
    this.props 表述component的属性,不可变

  4. React 生命周期

Mounting
These methods are called when an instance of a component is being created and inserted into the DOM:
constructor()
componentWillMount()
render()
componentDidMount()

Updating
An update can be caused by changes to props or state. These methods are called when a component is being re-rendered:
componentWillReceiveProps()
shouldComponentUpdate()
componentWillUpdate()
render()
componentDidUpdate()

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

推荐阅读更多精彩内容

  • react 基本概念解析 react 的组件声明周期 react 高阶组件,context, redux 等高级...
    南航阅读 1,090评论 0 1
  • 原教程内容详见精益 React 学习指南,这只是我在学习过程中的一些阅读笔记,个人觉得该教程讲解深入浅出,比目前大...
    leonaxiong阅读 2,860评论 1 18
  • 关于JSX 考虑这样一段代码:const element = Hello, world! ;这段代码既不是字符串...
    带三本书阅读 386评论 0 1
  • 现在最热门的前端框架,毫无疑问是 React 。上周,基于 React 的 React Native 发布,结果一...
    sakura_L阅读 442评论 0 0
  • 在一次电视节目中,主持人给了在场的嘉宾这样一道选择题:如果二者只能选一,那么你是愿意做一个博学成功而无法与人交流的...
    架构师米洛阅读 379评论 0 1