【问题标题】:GetInitialState Setting One Element But Not The OtherGetInitialState 设置一个元素但不设置另一个
【发布时间】:2016-02-16 11:59:03
【问题描述】:

我的 React 代码中看似不合理的行为。我在其中设置了两个元素,以在一行代码之后声明元素。我已经 console.log'ed 每个。两者都在分配之前正确打印,但只有比特币钱包变量(钱包)在状态中正确表示,而不是 imageLink。

module.exports = React.createClass({
   getInitialState: function() {
     return {
       walletAddress: null,
       image: null,
    };
   }, 
   componentWillMount: function() {
    Parse.User.currentAsync()
     .then((user) => { 
      var wallet = user.get('address');
      var imageLink = `https://api.qrserver.com/v1/create-qr-code/?size=500x500&data=bitcoin:${wallet}`;
      console.log("wallet", wallet);
      console.log("image: ", imageLink);
      this.setState({image: imageLink});
      this.setState({walletAddress: wallet});
      console.log("imagezzz: ", this.state.imageLink);
   })
 },

【问题讨论】:

  • 应该补充一下,我还尝试在一个分配中设置两个状态元素,即:this.setState({image: imageLink, walletAddress: wallet});。这也没有改变结果。
  • 看起来您正在记录state.imageLink(在console.log("imagezzz"... 行),但您正在设置state.image

标签: javascript reactjs react-native qr-code


【解决方案1】:

非常基本的错误,我分配给 this.state.image 并尝试记录 this.state.imageLink。

我的语法错误,其他一切正常。感谢@James Allardice 的澄清评论。

【讨论】:

    猜你喜欢
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    相关资源
    最近更新 更多