Nurdin
Nurdin

Reputation: 23883

React Native - How to initialize object in class component?

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

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.com/datomnurdin/blockchain-reactnative

Upvotes: 1

Views: 10215

Answers (2)

Jordan Daniels
Jordan Daniels

Reputation: 5304

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/>
    );
  }
}

Upvotes: 2

mediaguru
mediaguru

Reputation: 1917

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

Upvotes: 0

Related Questions