[关闭]
@duanyubin 2015-06-04T08:39:39.000000Z 字数 2547 阅读 305

React In Action

javascript


  1. Feature
    • Just the UI
    • Virtual DOM
  2. Props VS State
  3. Lifecycle
  4. Some tips
    • Form
    • Assign a key to dynamic Children
    • DOM event listener and ajax
    • Maintain classname and inline style
    • Communicate between components
    • Render method
  5. 4 examples

Feature

  1. var Wrapper = React.createClass({
  2. render: function(){
  3. var className = "inner"
  4. return (
  5. <div>
  6. This is Wrapper
  7. <Inner className={className} />
  8. </div>
  9. )
  10. }
  11. })
  12. var Inner = React.createClass({
  13. render: function(){
  14. return (
  15. <div className={this.props.className}>
  16. This is inner, className is {this.props.className}
  17. </div>
  18. )
  19. }
  20. })
  21. React.render(<Wrapper />, document.body);

Props VS State

Difference between props and state

Simply ask three questions about each piece of data:
1. Does it change over time? If not, it probably isn't state. (在某一时间点改变值)
2. Is it passed in from a parent via props? If so, it probably isn't state.(不来自于父元素)
3. Can you compute it based on any other state or props in your component? If so, it's not state.(不能由其他值计算得出)

Question: If a value passed in from a parent via props, but it will change in some time, is this state?

aaa

Lifecycle

  1. Mount -> Update -> Unmount

Tips

Form(input)

Assign a key to dynamic Children

  1. ...
  2. render: function(){
  3. var list = this.props.list
  4. return (
  5. <div>
  6. {
  7. list.map(function(item){
  8. return <Other item={item} key={item.id} />
  9. })
  10. }
  11. </div>
  12. )
  13. }
  14. ...

DOM event listener and ajax

Bind in componentDidMount method and unbind in componentWillUnmount

Maintain classname and inline style

  1. ...
  2. render: function(){
  3. var className = "aa"
  4. if(..) className += ' bb'
  5. return <div className={className} />
  6. }
  7. ...

Communicate between components

  1. For parent-child communication, simply pass props.
  2. For child-parent communication

    1. var GroceryList = React.createClass({
    2. handleClick: function(i) {
    3. console.log('You clicked: ' + this.props.items[i]);
    4. },
    5. render: function() {
    6. return (
    7. <div>
    8. {this.props.items.map(function(item) {
    9. return (
    10. <div onClick={this.handleClick.bind(this, i)} key={item}>{item}</div>
    11. );
    12. }, this)}
    13. </div>
    14. );
    15. }
    16. });
    17. React.render(
    18. <GroceryList items={['Apple', 'Banana', 'Cranberry']} />, mountNode
    19. );
  3. For communication between two components that don't have a parent-child relationship
    Use your own event system

Render method

In JSX, always return one element

  1. render: function(){
  2. return (
  3. <article />
  4. <article />
  5. )
  6. }
  1. render: function(){
  2. return (
  3. <section>
  4. <article />
  5. <article />
  6. </section>
  7. )
  8. }

Examples

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注