变量范围React native

时间:2018-06-17 23:13:05

标签: javascript node.js react-native

大家好我正在开展一个项目,在那里我使用websocket从arduino获取数据并根据数据更改显示真实(显示绿色圆圈)其他(显示红色圆圈),我尝试了太多东西但是似乎存储Variables(temp和val)的值与函数socket.on(Led_1)的范围保持一致,即使它们是全局变量并且我在这里失去理智。图像Terminal / APP VIEW



import React,{Component} from 'react';
import {Text,View,AppRegistry,StyleSheet,Image} from 'react-native';
import {Icon,Header} from 'react-native-elements';
const io = require('socket.io-client');
const socket = io.connect('http://41.251.107.235/');
var val;
var temp;
export default class VuePrincipale extends Component{
    constructor(){
        super();
        this.state ={
            value: false,
            color: '#ff0000',
            Temperature: 50
        };
        socket.on('Led_1',function(data){
          val = data.message;
          if(val){
            temp = 10;
          }else{
            temp = 0;
          }
          console.log(temp);
          console.log(val);
        });
    }

    displayState_Led1(){
        if(temp)
        {
          return(<Icon reverse type='ionicon' color='#0cfc84'/>);
        }
        else{
          return(<Icon reverse type='ionicon' color='#f26a6a'/>);
        }
      }
      displayState_Led2(){
        if(temp)
        {
          return(<Icon reverse type='ionicon' color='#0cfc84'/>);
        }
        else{
          return(<Icon reverse type='ionicon' color='#f26a6a'/>);
        }
      };
      displayState_Led3(){
        if(temp)
        {
          return(<Icon reverse type='ionicon' color='#0cfc84'/>);
          console.log('TRUE FROM IF')
        }
        else{
          return(<Icon reverse type='ionicon' color='#f26a6a'/>);
        }
      }
&#13;
&#13;
&#13;

0 个答案:

没有答案