react-native

react native 常用技巧

2021-05-06  本文已影响0人  mark666

1.设置阴影效果

引入

import {Card} from 'react-native-shadow-cards'

使用

 <Card style={{width:width - 60,marginLeft:20}} cornerRadius={5} opacity={0.3} elevation={2}>
        <View style={{ flexDirection:'row',width:width-60,justifyContent:'center'}}>
            <CartVButton url={require('../../img/bg-face.png')} onPress={this._onPressItem} label="退货单"/>
            <CartVButton url={require('../../img/bg-face.png')} onPress={this._onPressItem} label="退货单"/>
            <CartVButton url={require('../../img/bg-face.png')} onPress={this._onPressItem} label="退货单"/>
        </View>
 </Card>
示例

2.修改Flatlist数据视图不刷新解决方案

给flatlist添加属性:

handleMethod = {({viewableItems}) => 
this.handleViewableItemsChanged(viewableItems)}

3. TextInput 输入框 设置

<TextInput
              style={styles.input}
              underlineColorAndroid="transparent" // 安卓下划线颜色
              placeholder="请输入账号"
              placeholderTextColor='#858585'
              maxLength={20}
              onChangeText={(text)=>{
                this.setState({
                  showUsernameLengthAlert:false,
                  username:text
                },()=>{
                  this._disabledLoginBtn()
                })
              }}
              value={this.state.username}
              onBlur={this._accountOnBlur.bind(this)}
 secureTextEntry={!this.state.showPassword}
              maxLength={20}
            />

input:{
    paddingHorizontal:0,
    paddingVertical:0,
}

4. 滚动视图点击穿透以及不可滚动

 <ScrollView
          keyboardShouldPersistTaps="handled"
          scrollEnabled={false}
        >

5. ReactNative之解决文件导入路径问题

/**
 * @providesModule Common
 */

import {
    Dimensions
} from 'react-native';

export default class Common {

    static bgColor = 'rgb(232,232,232)';

    static screenW = Dimensions.get('window').width;

    static screenH = Dimensions.get('window').height;
}
// 以前需要这样
// import Common from './../Common/Common'

// 现在可以直接用类名
import Common from 'Common'

6 .过滤掉数组中有重复属性的对象

const demo = [
  {id:1,value:20},
 {id:2,value:20},
 {id:1,value:40},
]
import _ from 'lodash'
const result = _.uniqBy(demo,'id')
----
 [{id:1,value:20},
 {id:2,value:20},]

7、设置随机的颜色

`#${(`#00000${(Math.random() * (1 << 24) | 0).toString(16)}`).slice(-6)}`

8. ios ScrollView 滚动条位置偏离

增加属性设置

scrollIndicatorInsets={{ right: 1 }}
上一篇 下一篇

猜你喜欢

热点阅读