美文网首页
React 笔记

React 笔记

作者: MelodyIsUVoice | 来源:发表于2017-04-29 08:59 被阅读25次

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 笔记

      本文链接:https://www.haomeiwen.com/subject/lwwozttx.html