javascript - React Native - How to initialize object in class component? - Stack Overflow

I got this error message when I'm trying to initialize an object in the class ponent.Error message

I got this error message when I'm trying to initialize an object in the class ponent.

Error message

    Failed to load bundle(http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false) with error:(SyntaxError: /blockchain/node_modules/react-native/App.js: Unexpected token (9:6)

My code

App.js

import Block from './block.js'

export default class App extends Component {

  let genesisBlock = new Block(); //error here
  let blockchain = new Blockchain(genesisBlock);

  render() {
    return (
      </View>

    );
  }
}

block.js

export default class Block {
    constructor() {
        this.index = 0
        this.previousHash = ""
        this.hash = ""
        this.nonce = 0
        this.transactions = []
     }

     addTransaction(transaction) {
        this.transactions.push(transaction)
    }

    get key() {
        return JSON.stringify(this.transactions) + this.index + this.previousHash + this.nonce
    }
}

But if I remove let, it said variable genesisBlock not found.

Reference:

I got this error message when I'm trying to initialize an object in the class ponent.

Error message

    Failed to load bundle(http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false) with error:(SyntaxError: /blockchain/node_modules/react-native/App.js: Unexpected token (9:6)

My code

App.js

import Block from './block.js'

export default class App extends Component {

  let genesisBlock = new Block(); //error here
  let blockchain = new Blockchain(genesisBlock);

  render() {
    return (
      </View>

    );
  }
}

block.js

export default class Block {
    constructor() {
        this.index = 0
        this.previousHash = ""
        this.hash = ""
        this.nonce = 0
        this.transactions = []
     }

     addTransaction(transaction) {
        this.transactions.push(transaction)
    }

    get key() {
        return JSON.stringify(this.transactions) + this.index + this.previousHash + this.nonce
    }
}

But if I remove let, it said variable genesisBlock not found.

Reference: https://github./datomnurdin/blockchain-reactnative

Share Improve this question edited Dec 22, 2018 at 13:04 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Dec 18, 2018 at 21:25 NurdinNurdin 23.9k47 gold badges140 silver badges315 bronze badges 2
  • genesisBlock = new Block(); ... no let needed (or var) – Wainage Commented Dec 18, 2018 at 21:45
  • already done but still same. any sample code? – Nurdin Commented Dec 18, 2018 at 21:48
Add a ment  | 

2 Answers 2

Reset to default 2

Try:

import Block from './block.js'

export default class App extends Component {
  constructor(){
    super()
    this.genesisBlock = new Block();
    this.blockchain = new Blockchain(this.genesisBlock);
  }

  render() {
    return (
      <View/>
    );
  }
}

Your render has a closing View tag with no opening one.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745600299a4635362.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信