gpt4 book ai didi

javascript - React Native - 如何在类组件中初始化对象?

转载 作者:行者123 更新时间:2023-11-30 14:14:22 25 4
gpt4 key购买 nike

当我尝试在类组件中初始化一个对象时收到此错误消息。

错误信息

    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)

我的代码

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
}
}

但是如果我删除let,它会说找不到变量genesisBlock

引用:https://github.com/datomnurdin/blockchain-reactnative

最佳答案

尝试:

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

关于javascript - React Native - 如何在类组件中初始化对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53841315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com