ReactJS中的自定义组件实例代码

所属分类: 网络编程 / 相关技巧 阅读数: 1659
收藏 0 赞 0 分享

React 是一个用于构建用户界面的 JAVASCRIPT 库。

React 主要用于构建UI,很多人认为 React 是 MVC 中的 V(视图)。

React 起源于 Facebook 的内部项目,用来架设 Instagram 的网站,并于 2013 年 5 月开源。

React 拥有较高的性能,代码逻辑非常简单,越来越多的人已开始关注和使用它。

React 特点

1.声明式设计 −React采用声明范式,可以轻松描述应用。

2.高效 −React通过对DOM的模拟,最大限度地减少与DOM的交互。

3.灵活 −React可以与已知的库或框架很好地配合。

4.JSX − JSX 是 JavaScript 语法的扩展。React 开发不一定使用 JSX ,但我们建议使用它。

5.组件 − 通过 React 构建组件,使得代码更加容易得到复用,能够很好的应用在大项目的开发中。

6.单向响应的数据流 − React 实现了单向响应的数据流,从而减少了重复代码,这也是它为什么比传统数据绑定更简单。

可控自定义组件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <script src="js/react.js"></script>
    <script src="js/react-dom.js"></script>
    <script src="js/browser.min.js"></script>
  </head>
  <body>
    <script type="text/babel">
      var Radio=React.createClass({
        getInitialState:function(){
          return {
            value:this.props.defaultValue
          };
        },
        handleChange:function(event){
          if(this.props.onChange){
            this.props.onChange(event);
          }
          this.setState({
            value:event.target.value
          });
        },
        render:function(){
          var children=[];
          var value=this.props.value||this.state.value;
          React.Children.forEach(this.props.children,function(child,i){
            var label=<label key={i}>
              <input type="radio" name={this.props.name} value={child.props.value} checked={child.props.value==value} onChange={this.handleChange}/>
              {child.props.children}
              <br/>
          </label>;
          children.push(label);
          }.bind(this));
          return <span>{children}</span>;
        }
      });
      var MyForm=React.createClass({
        getInitialState:function(){
          return ({my_radio:"B"});
        },

        handleChange:function(event){
          this.setState({
            my_radio:event.target.value
          });
        },
        submitHandler:function(event){
          event.preventDefault();
          alert(this.state.my_radio);
        },
        render:function(){
          return (
            <form onSubmit={this.submitHandler}>
            <Radio value={this.state.my_radio} name="my_radio" onChange={this.handleChange}>
              <option value="A">First option</option>
              <option value="B">Second option</option>
              <option value="C">Third option</option>
            </Radio>
            <button type="submit">Speak</button>
            </form>
          )
        }
      });

      ReactDOM.render(<MyForm></MyForm>,document.body);
    </script>
  </body>
</html>

不可控的自定义组件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <script src="js/react.js"></script>
    <script src="js/react-dom.js"></script>
    <script src="js/browser.min.js"></script>
  </head>
  <body>
    <script type="text/babel">
      var Radio=React.createClass({
        getInitialState:function(){
          return {
            value:this.props.defaultValue
          };
        },
        handleChange:function(event){
          if(this.props.onChange){
            this.props.onChange(event);
          }
          this.setState({
            value:event.target.value
          });
        },
        render:function(){
          var children=[];
          var value=this.props.value||this.state.value;
          React.Children.forEach(this.props.children,function(child,i){
            var label=<label>
              <input type="radio" name={this.props.name} value={child.props.value} checked={child.props.value==value} onChange={this.handleChange}/>
              {child.props.children}
              <br/>
          </label>;
          children['label'+i]=label;
          }.bind(this));
          return <span>{children}</span>;
        }
      });
      var MyForm=React.createClass({
        handleSubmit:function(event){
          event.preventDefault();
          alert(this.refs.radio.state.value);
        },
        render:function(){
          return (
            <form onSubmit={this.handleSubmit}>
            <Radio ref="radio" name="my_radio" defaultValue="B">
              <p value="A">First</p>
              <option value="B">Second option</option>
              <option value="C">Third option</option>
            </Radio>
            <button type="submit">Speak</button>
            </form>
          )
        }
      });

      ReactDOM.render(<MyForm></MyForm>,document.body);
    </script>
  </body>
</html>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <script src="js/react.js"></script>
    <script src="js/react-dom.js"></script>
    <script src="js/browser.min.js"></script>
  </head>
  <body>
    <script type="text/babel">
      var Radio=React.createClass({
        getInitialState:function(){
          return {
            value:this.props.defaultValue
          };
        },
        handleChange:function(event){
          if(this.props.onChange){
            this.props.onChange(event);
          }
          this.setState({
            value:event.target.value
          });
        },
        render:function(){
          var children=[];
          var value=this.props.value||this.state.value;
          React.Children.forEach(this.props.children,function(child,i){
            var label=<label>
              <input type="radio" name={this.props.name} value={child.props.value} checked={child.props.value==value} onChange={this.handleChange}/>
              {child.props.children}
              <br/>
          </label>;
          children['label'+i]=label;
          }.bind(this));
          return <span>{children}</span>;
        }
      });
      var MyForm=React.createClass({
        handleSubmit:function(event){
          event.preventDefault();
          alert(this.refs.radio.state.value);
        },
        render:function(){
          return (
            <form onSubmit={this.handleSubmit}>
            <Radio ref="radio" name="my_radio" defaultValue="B">
              <p value="A">First</p>
              <option value="B">Second option</option>
              <option value="C">Third option</option>
            </Radio>
            <button type="submit">Speak</button>
            </form>
          )
        }
      });

      ReactDOM.render(<MyForm></MyForm>,document.body);
    </script>
  </body>
</html>

以上所述是小编给大家介绍的ReactJS中的自定义组件实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

更多精彩内容其他人还在看

程序员编程从初级到中级的10个秘诀

在一封与TechRepublic会员交流的邮件当中,他提到了面向程序员的博客、文章及杂志分成两类:面向初学者类(“hello world”这种类型的教程)以及面向专家类(MSDN杂志)。
收藏 0 赞 0 分享

关于换行和回车的图文小结

不知道大家用这么多年的电脑,写了这么多的程序,对换行和回车有没有一个认识
收藏 0 赞 0 分享

回车和换行有什么区别?我们平时按下的Enter键是回车还是换行

如果用过机械打字机,就知道回车和换行的区别了。换行就是把滚筒卷一格,不改变水平位置。回车就是把水平位置复位,不卷动滚筒
收藏 0 赞 0 分享

网络编程之get与post的区别与联系

这里来说说get与post的区别与联系,对这方面不懂的鹏哟可以参考下。
收藏 0 赞 0 分享

Web开发人员常用速查手册 英文集合推荐

不管你是多么优秀的程序员,你都不可能记住一切。在你编写程序的过程中碰到问题需要查阅手册的时候,若有现成的手册可参考则可以为你节省很多时间。
收藏 0 赞 0 分享

vs快捷键 用好Ctrl+Enter与Ctrl+Shift+Enter组合键让你的编辑代码速度快了很多

使用Ctrl+Enter组合键在上方插入一行,使用Ctrl+Shift+Enter组合键在下方插入一行
收藏 0 赞 0 分享

对Web开发人员有用的8个网站小结

本文是由比利时的Web开发人员Jean-Baptiste Jung分享的,Jung还在《Web开发/设计人员应当知道的15个网站》这篇文章中推荐了15个相关网站
收藏 0 赞 0 分享

Web开发/设计人员应当知道的15个网站

建个好网站绝非易事,工欲善其事必先利其器。本文编译了15个极其有用的网站,任何一位网站开发者或设计人员都应该收藏起来
收藏 0 赞 0 分享

Application,Session,Cookies对象应用介绍

Application,Session,Cookies对象比较,对于客户端保存登录信息的朋友可以参考下。
收藏 0 赞 0 分享

设计高可用和高负载的网站系统的几个注意事项

随着网站的运营,用户访问量和数据存储量会随着时间发生几何级变化,很快整个系统不堪重负,频繁出现问题。
收藏 0 赞 0 分享
查看更多