zoukankan      html  css  js  c++  java
  • React(JSX语法)-----JSX属性

    1. if you know all the propertities that you want to place on a component ahead of time,it is easy to use JSX:

    var component = <Component foo={x} bar={y}/>
    

    2. This is an anti-pattern (反面实例---错误的) because it means that we can't help you check the right propTypes until way later.This means that your propTypes errors end up with a cryptic stack trace.

    var component = <Component/>;
    component.props.foo = x; //bad
    component.props.bar = y;//also bad
    

      the props should be considered immutable at this point(认为是不可改变的).Mutating the props object somewhere else could cause unexpected consequences so ideally it would be a frozen object at this point.

    3.you can use a new feature of JSX called spread attributes:

    var props = {};
    props.foo = x;
    props.bar = y;
    var component = <Component {...props}>;
    

      the properties of the object that you pass in are copied onto the component's props.you can use this multiple times or combine it with other attributes.The specification order id important.Later attributes override previous ones;

    var props = { foo: 'default' };
      var component = <Component {...props} foo={'override'} />;
      console.log(component.props.foo); // 'override'
    

      

  • 相关阅读:
    [题解] [HNOI2014] 世界树
    [luogu 5301][bzoj 5503] [GXOI/GZOI2019] 宝牌一大堆
    [HDU4507]吉哥系列故事——恨7不成妻
    [国家集训队]聪聪可可
    [模板]点分治
    [2018.8.12]模拟赛B组
    JZOJ5804. 【2018.08.12提高A组模拟】简单的序列
    2018.8.10模拟赛
    2018.8.8模拟赛
    [2018.8.6]模拟赛
  • 原文地址:https://www.cnblogs.com/jodie-blog/p/5109672.html
Copyright © 2011-2022 走看看