国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

ReactJS中的自定義組件實(shí)例代碼

2024-07-21 02:03:10
字體:
供稿:網(wǎng)友

React 是一個(gè)用于構(gòu)建用戶界面的 JAVASCRIPT 庫。

React 主要用于構(gòu)建UI,很多人認(rèn)為 React 是 MVC 中的 V(視圖)。

React 起源于 Facebook 的內(nèi)部項(xiàng)目,用來架設(shè) Instagram 的網(wǎng)站,并于 2013 年 5 月開源。

React 擁有較高的性能,代碼邏輯非常簡(jiǎn)單,越來越多的人已開始關(guān)注和使用它。

React 特點(diǎn)

1.聲明式設(shè)計(jì) −React采用聲明范式,可以輕松描述應(yīng)用。

2.高效 −React通過對(duì)DOM的模擬,最大限度地減少與DOM的交互。

3.靈活 −React可以與已知的庫或框架很好地配合。

4.JSX − JSX 是 JavaScript 語法的擴(kuò)展。React 開發(fā)不一定使用 JSX ,但我們建議使用它。

5.組件 − 通過 React 構(gòu)建組件,使得代碼更加容易得到復(fù)用,能夠很好的應(yīng)用在大項(xiàng)目的開發(fā)中。

6.單向響應(yīng)的數(shù)據(jù)流 − React 實(shí)現(xiàn)了單向響應(yīng)的數(shù)據(jù)流,從而減少了重復(fù)代碼,這也是它為什么比傳統(tǒng)數(shù)據(jù)綁定更簡(jiǎn)單。

可控自定義組件:

<!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>

總結(jié)

以上所述是小編給大家介紹的ReactJS中的自定義組件實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 堆龙德庆县| 大化| 舒兰市| 曲阜市| 杭锦后旗| 买车| 手机| 田阳县| 江阴市| 库车县| 临颍县| 会昌县| 禄劝| 靖安县| 布拖县| 金昌市| 达日县| 勃利县| 横峰县| 望江县| 武邑县| 甘肃省| 安乡县| 获嘉县| 玉环县| 仙居县| 云梦县| 康保县| 苍山县| 隆化县| 马鞍山市| 柳江县| 阿克陶县| 五常市| 本溪市| 深水埗区| 常德市| 泗洪县| 宁波市| 宜丰县| 钦州市|