Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何更新全局变量react native的值?_Javascript_Reactjs_React Native_Global Variables_Local Variables - Fatal编程技术网

Javascript 如何更新全局变量react native的值?

Javascript 如何更新全局变量react native的值?,javascript,reactjs,react-native,global-variables,local-variables,Javascript,Reactjs,React Native,Global Variables,Local Variables,我想更新bjpv的值,并最终将其状态设置为新的更新状态,我将从firebase函数中获得该状态 我正在将expo用于我的react本机应用程序。这是一个简单的投票应用程序 class Dashboard extends React.Component { constructor(props){ super(props) this.state = ({ name: '', bjp:'', }) this.componentDidMou

我想更新
bjpv
的值,并最终将其状态设置为新的更新状态,我将从
firebase
函数中获得该状态

我正在将expo用于我的react本机应用程序。这是一个简单的投票应用程序

class Dashboard extends React.Component {
constructor(props){
    super(props)
    this.state = ({
        name: '',
        bjp:'',
    })
    this.componentDidMount=()=>{
        var bjpv;
        console.log("var bjpv loaded")
        firebase.database().ref("candid/BJP").once('value').then(function(snapshot){
            ping = snapshot.val().votes;
            // return bjpv;
            console.log("Start firebase");
            console.log(bjpv);
            console.log(ping);
            this.setState.bjpv = this.setState.ping;
            console.log("Closing firebase")
        })
        console.log("No fire");
        console.log(bjpv);
        console.log("End message")
        this.setState(function(state, props){
            console.log("Hello")
            return {
                bjp: bjpv,
            }
        })
}
}
我想访问这个常量数组中的值

var data = [
    { name: 'BJP', votes : this.state.bjp, color: 'pink', legendFontColor: '#7F7F7F', legendFontSize: 10 }]

最后,我想更新从
firebase

获取的var数据中的投票值,我试图理解您的代码。这就是你的意思吗

class Dashboard extends React.Component {
  state = {
    name: '',
    bjp: ''
  }

  componentDidMount() {
    firebase.database().ref("candid/BJP").once('value')
      .then(snapshot => {
        let bjp = snapshot.val().votes;
        this.setState({ bjp });
      })
  }
}

请重新编写代码。非常感谢@roel。代码正在运行。还有一件事你能解释一下我哪里做错了吗?这行:
This.setState.bjpv=This.setState.ping
。我想你的意思是
this.state.bjpv=this.state.ping
,因为
setState
是一个函数,但它也不起作用。不能直接更改状态变量,必须使用
setState
更改状态变量。