react native 之setState

react native 版本采用0.33版本。。新版本相当于旧版本有了一部分改动,还是比较麻烦的,这里重新开始学习。

react native 结构:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

class BleManager extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('BleManager', () => BleManager);

这是一个最基础的代码结构,react native的精髓就在于state、props,我一直没搞懂,主要0.33跟以前的版本有些不太一样了,初始化state一定要这样写

	constructor() {  
		super();
    	        this.state = {  
      		    isRefreshing: false,  
      		};  
    	};  

在后面的代码里面通过setState来修改state来达到修改dom也就是render()里面的内容代码结构,这里可以看一段代码


	_onRefresh() { 
	  	this.setState({isRefreshing: true});  
    	setTimeout(() => {  
	      	// 准备下拉刷新的5条数据  
	      	const rowData = Array.from(new Array(5))  
	     		.map((val, i) => ({  
	      		 text: '刷新行 ' + (+this.state.loaded + i)  
	      	})).concat(this.state.rowData);  
	   
	      	this.setState({  
	        	loaded: this.state.loaded + 5,  
	        	isRefreshing: false,  
	        	rowData: rowData,  
	      	});  
	    }, 5000);  
    }

render() {
  		let rows = this.state.rowData.map((row,ii) => {  
	    	return <Row key={ii} data={row}/>;  
	    }); 
	    
   		return (
        	<ScrollView style={styles.scrollview}
		        refreshControl={  
		          	<RefreshControl  
		           		refreshing={this.state.isRefreshing}  
		            	onRefresh={this._onRefresh.bind(this)}  
		            	colors={['#ff0000', '#00ff00','#0000ff','#3ad564']}  
		           		progressBackgroundColor="#ffffff"  
		          	/>  
		        }>  
		        
		        <ViewPager  
				    style={{height:130}}  
				    dataSource={this.state.dataSource}  
				    renderPage={this._renderPage.bind(this)}  
				    isLoop={true}  
				    autoPlay={true}/>  
            	{rows}
            </ScrollView>
    	);
  	}

在0.33以后写法一定要切记在调用onRefresh={this._onRefresh.bind(this)} ,如果后面没有bind(this),那么会对setState提示报错,问题纠结了很久,OK,今天先就讲解state,欢迎大家交流

转载于:https://my.oschina.net/u/163029/blog/748787

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native 是一个跨平台的移动应用开发框架,可以使用JavaScript编写原生的iOS和Android应用程序。在React Native中,我们可以使用温度传感器来获取当前环境的温度信息。 要使用温度传感器,我们首先需要安装react-native-sensors库。然后,在React Native的代码中,我们可以导入温度传感器组件,并通过订阅温度传感器事件来获取当前的温度。 代码示例如下: ```javascript import { DeviceEventEmitter } from 'react-native'; import { Thermometer } from 'react-native-sensors'; export default class App extends React.Component { constructor(props) { super(props); this.state = { temperature: 0, }; } componentDidMount() { const thermometer = new Thermometer(); thermometer.subscribe(({ temperature }) => { this.setState({ temperature }); }); } render() { return ( <View style={styles.container}> <Text style={styles.temperature}>{this.state.temperature}℃</Text> </View> ); } } ``` 在上面的代码中,我们使用了Thermometer组件和DeviceEventEmitter来订阅温度传感器事件,并将获取到的温度信息更新到组件的状态中。然后我们可以将温度信息渲染到界面上。 需要注意的是,使用温度传感器可能需要在设备上具备相应的硬件和权限。在运行React Native应用之前,需要确认设备上是否具备温度传感器,并为应用程序授予获取温度传感器数据的权限。 总结起来,React Native可以使用温度传感器库来获取当前环境的温度信息,通过订阅温度传感器事件,将温度信息更新到应用程序的界面中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值