react native 知識點積累

1.綁定this

export default class App extends React.Component {
  constructor(props){
    super(props)
    this.state = {
      numb : 33
    }
    //自定義方法需要自己手動綁定this,否則沒法調(diào)用 this.state
    this.sumAwithB = this.sumAwithB.bind(this);
  }

  sumAwithB(){
    this.setState({
      numb:this.state.numb + 1
    })
  }
  
  render(){
    return(
      <View style = {{flex:1,justifyContent:'center',alignItems:'center',backgroundColor:'#ccf'}}>
      <Text>ee說說ee+:{this.state.numb}</Text>
      <Button 
      title='cddd'
      onPress={this.sumAwithB}></Button>
      </View>
    )
  }
}

如果直接寫成 箭頭函數(shù),就不用綁定this

export default class App extends React.Component {
  constructor(props){
    super(props)
    this.state = {
      numb : 33
    }
  }
  
  render(){
    return(
      <View style = {{flex:1,justifyContent:'center',alignItems:'center',backgroundColor:'#ccf'}}>
      <Text>eee說說eee+:{this.state.numb}</Text>
      <Button 
      title='cddd'
      onPress={()=>{
        this.setState({
          numb: this.state.numb+1
        })
      }}></Button>
      </View>
    )
  }
}

2.react native 默認導(出)入和普通導(出)入 區(qū)別
(1). 默認導出 export class, 導入 import FoodMenu from './FoodMenu';
(2).普通導出 export, 導入 導入 import {FoodMenu} from './FoodMenu';
(3).
a.默認: 命名導入 impobrt foodM from './FoodMenu';
b.普通: 命名導入 import {FoodMenu as foodM } from './FoodMenu';

3.經(jīng)緯度
a. 模擬器設置經(jīng)緯度的方法:
啟動模擬器,選擇頭部中的Debug-Location-Custom Location
也就是按照下面的順序點擊就行了 iOS Simulator > Features > Location > Custom Location

image

b.導入庫,

cd  ./項目根目錄
yarn add @react-native-community/geolocation 

cd ios
pod install


具體實現(xiàn)代碼
import geolocation from '@react-native-community/geolocation';
componentDidMount(){
        //彈出手機所在的位置的經(jīng)緯度
        //H5中提供了,navigator.geolocation,讓我們獲取手機坐標
        navigator.geolocation = geolocation;
        navigator.geolocation.getCurrentPosition((pos)=>{
            //這個函數(shù)在,獲取完這個經(jīng)緯度之后來執(zhí)行
            const coords = pos.coords //獲取坐標對象
            //彈出經(jīng)度和維度
            alert(coords.longitude+","+coords.latitude)
        })
    }

4 ref > 16.3 禁用字符串,正確寫法
官方解釋 https://reactjs.org/docs/refs-and-the-dom.html

constructor(props){
    super(props)
    this.scrollVRef = React.createRef();
  }
componentDidMount(){
    const scrollV = this.scrollVRef.current;
  }
render(){
    return(
      <View style = {{marginTop:44}}>
        <ScrollView 
          ref={this.scrollVRef}
          horizontal 
          pagingEnabled
          showsHorizontalScrollIndicator={false}//滾動條
        >
        <View style={styles.box}>
        </View>
        </ScrollView>
      </View>
    )
  }

5.swiper 在外圍添加一個盒子,設置高度
6.Image 設置 寬高 才能展示

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容