晴明的博客园 GitHub      CodePen      CodeWars     

[react] 动态JSX标签

  • 原本的写法
const input = this.props.long ? <textarea
  onChange={this.props.onChange}
  className="make-this-pretty"
  id="important-input"
  name="important-input"
/> : <input
  onChange={this.props.onChange} 
  className="make-this-pretty"
  id="important-input"
  name="important-input"
/>;
  • 改进的写法
const Tag = this.props.long ? "textarea" : "input";

const input = <Tag 
                  onChange={this.props.onChange}
                  className="make-this-pretty"
                  id="important-input"
              />;

posted @ 2017-02-20 18:58  晴明桑  阅读(2233)  评论(0)    收藏  举报