最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin5PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far