我正在努力使用React中的一个对象,我试图访问正在返回的键:值(我可以成功地console.log)。
我尝试过的所有示例都会导致映射每个字符或抛出对象子错误,我感到很困惑。
componentDidMount() {
this.gun.on('auth', () => this.thing());
}
thing() {
this.gun.get('todos').map((value, key) => { console.log(key, value) }
);
}
handleChange = e => this.setState({ newTodo: e.target.value })
add = e => {
e.preventDefault()
this.gun.get('todos').set(this.state.newTodo)
this.setState({ newTodo: '' })
}
del = key => this.gun.get(key).put(null)
render() {
return <>
<Container>
<div>Gun</div>
<div>
<form onSubmit={this.add}>
<input value={this.state.newTodo} onChange={this.handleChange} />
<button>Add</button>
</form>
<br />
<ul>
{this.state.todos.map(todo => <li key={todo.key} onClick={_ => this.del(todo.key)}>{todo.val}</li>)}
</ul>
</div>
</Container></>
}
}
gun
对象已经正确初始化了吗? - Dacre Dennygun
对象所做的更改? - Dacre Denny