欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

es6在react中的應用代碼解析

 更新時間:2017年11月08日 09:25:56   投稿:mrr  
這篇文章主要介紹了es6在react中的應用代碼解析,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

不論是React還是React-native,facebook官方都推薦使用ES6的語法,沒在項目中使用過的話,突然轉(zhuǎn)換過來會遇到一些問題,如果還沒有時間系統(tǒng)的學習下ES6那么注意一些常見的寫法暫時也就夠用的,這會給我們的開發(fā)帶來很大的便捷,你會體驗到ES6語法的無比簡潔。下面給大家介紹es6在react中的應用,具體內(nèi)容如下所示:

import React,{Component} from 'react';
class RepeatArrayextends Component{
 constructor() {  super();
 }
 render(){
  const names = ['Alice', 'Emily', 'Kate'];
  return (
   <div>
   {
    names.map((name) =>{return <div>Hello, {name}!</div>;} )
   }
   </div>
);
}
}
export default RepeatArray;

二、ol與li的實現(xiàn)

import React,{Component} from 'react';
class RepeatLiextends Component{
 render(){
  return (
   <ol>
   {
    this.props.children.map((child)=>{return <li>{child}</li>})
   }
   </ol>
);
}
}
class RepeatArray extends Component{
constructor() {
super();
}
render(){
return (
<div>
<RepeatLi>
<span>hello</span>
    <span>world</span>
</RepeatLi>
   </div>
);
}
}
export default RepeatArray;

三、從服務端獲取數(shù)據(jù)

import React,{Component} from 'react';
class UserGistextends Component{
 constructor(){
  super();
  this.state={
   username:'',
   lastGistUrl:''
  }
 }
 componentWillMount(){
  $.get(this.props.source, function(result){
   var lastGist = result[0];
   //if (this.isMounted()) {
    this.setState({
     username: lastGist.owner.login,
     lastGistUrl: lastGist.html_url
    });
   //}
  }.bind(this));
 }
 render(){
  return(
   <div>
    {this.state.username} ..
    <a href={this.state.lastGistUrl} >here</a>
</div>
  );
 }
}
class RepeatArrayextends Component{
 constructor() {
  super();
 }
 render(){
  return (
   <div>
   <UserGist source="https://api.github.com/users/octocat/gists" />
   </div>
);
}
}
export default RepeatArray;

四、初始化STATE

class Videoextends React.Component{
  constructor(props){
    super(props);
    this.state = {
      loopsRemaining: this.props.maxLoops,
    };
  }
}

五、解構與擴展操作符

在給子組件傳遞一批屬性更為方便了。下面的例子把 className 以外的所有屬性傳遞給 div 標簽

class AutoloadingPostsGridextends React.Component{
  render() {
    var {
      className,
      ...others, // contains all properties of this.props except for className
    } = this.props;
    return (
      <div className={className}>
        <PostsGrid {...others} />
        <button onClick={this.handleLoadMoreClick}>Load more</button>
</div>
    );
  }
}

使用 react 開發(fā)最常見的問題就是父組件要傳給子組件的屬性較多時比較麻煩

class MyComponentextends React.Component{
//假設MyComponent已經(jīng)有了name和age屬性
 render(){
  return (
   <SubComponent name={this.props.name} age={this.props.age}/>
   )
 }
}

使用擴展操作符可以變得很簡單

class MyComponentextends React.Component{
//假設MyComponent已經(jīng)有了name和age屬性
 render(){
  return (
   <SubComponent {...this.props}/>
   )
 }
}

上述方式是將父組件的所有屬性都傳遞下去,如果這其中有些屬性我不需要傳遞呢?也很簡單

class MyComponentextends React.Component{
//假設MyComponent有很多屬性,而name屬性不需要傳遞給子組件
 var {name,...MyProps}=this.props;
 render(){
  return (
   <SubComponent {...Myprops}/>
   )
 }
}

上述方法最常用的場景就是父組件的 class 屬性需要被單獨提取出來作為某個元素的 class ,而其他屬性需要傳遞給子組件

六、創(chuàng)建組件

import React,{Component} from "react";
class MyComponentextends Component{
//組件內(nèi)部代碼
}

七、State/Props/PropTypes

es6 允許將 props 和 propTypes 當作靜態(tài)屬性在類外初始化

class MyComponentextends React.Component{}
MyComponent.defaultProps={
 name:"SunnyChuan",
 age:22
};
MyComponent.propTypes={
 name:React.PropTypes.string.isRequired,
 age:React.PropTypes.number.isRequired
};

es7 支持直接在類中使用變量表達式

class MyComponentextends React.Component{
 static defaultProps={
  name:"SunnyChuan",
  age:22
 }
 static propTypes={
  name:React.PropTypes.string.isRequired,
  age:React.PropTypes.number.isRequired
 }
}

state 和前兩個不同,它不是靜態(tài)的

class MyComponentextends React.Component{
 static defaultProps={
  name:"SunnyChuan",
  age:22
 }
 state={
   isMarried:false
 }
 static propTypes={
  name:React.PropTypes.string.isRequired,
  age:React.PropTypes.number.isRequired
 }
}

七、當你構建通用容器時,擴展屬性會非常有用

function App1(){
 return <GreetingfirstName="Ben"lastName="Hector"/>;
}
function App2() {
const props = {firstName: 'Ben', lastName: 'Hector'};
 return <Greeting {...props} />;
}

八、使用es6的計算屬性代替

this.setState({
  [name]:value
})
//代替
var partialState = {};
partialState[name] = value;
this.setState(partialState);

總結(jié)

以上所述是小編給大家介紹的es6在react中的應用代碼解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • 詳細談談React中setState是一個宏任務還是微任務

    詳細談談React中setState是一個宏任務還是微任務

    學過react的人都知道,setState在react里是一個很重要的方法,使用它可以更新我們數(shù)據(jù)的狀態(tài),下面這篇文章主要給大家介紹了關于React中setState是一個宏任務還是微任務的相關資料,需要的朋友可以參考下
    2021-09-09
  • react國際化化插件react-i18n-auto使用詳解

    react國際化化插件react-i18n-auto使用詳解

    這篇文章主要介紹了react國際化化插件react-i18n-auto使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • React?時間切片理解分析

    React?時間切片理解分析

    這篇文章主要為大家介紹了React?時間切片理解分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • 在?React?Native?中使用?CSS?Modules的配置方法

    在?React?Native?中使用?CSS?Modules的配置方法

    有些前端工程師希望也能像開發(fā) web 應用那樣,使用 CSS Modules 來開發(fā) React Native,本文將介紹如何在 React Native 中使用 CSS Modules,需要的朋友可以參考下
    2022-08-08
  • React組件實例三大核心屬性State props Refs詳解

    React組件實例三大核心屬性State props Refs詳解

    組件實例的三大核心屬性是:State、Props、Refs。類組件中這三大屬性都存在。函數(shù)式組件中訪問不到 this,也就不存在組件實例這種說法,但由于它的特殊性(函數(shù)可以接收參數(shù)),所以存在Props這種屬性
    2022-12-12
  • 淺談React + Webpack 構建打包優(yōu)化

    淺談React + Webpack 構建打包優(yōu)化

    本篇文章主要介紹了淺談React + Webpack 構建打包優(yōu)化,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • React 實現(xiàn)車牌鍵盤的示例代碼

    React 實現(xiàn)車牌鍵盤的示例代碼

    這篇文章主要介紹了React 實現(xiàn)車牌鍵盤的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-12-12
  • react學習筆記之state以及setState的使用

    react學習筆記之state以及setState的使用

    這篇文章主要介紹了react學習筆記之state以及setState的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • React-router?v6在Class組件和非組件代碼中的正確使用

    React-router?v6在Class組件和非組件代碼中的正確使用

    這篇文章主要介紹了React-router?v6在Class組件和非組件代碼中的正確使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • react的滑動圖片驗證碼組件的示例代碼

    react的滑動圖片驗證碼組件的示例代碼

    這篇文章主要介紹了react的滑動圖片驗證碼組件的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-02-02

最新評論