如何在 this.state 中添加对象和变量(变量已在类外定义)?如果我只添加变量(initialState),它可以正常工作。但是当我添加一个额外的对象时,它不起作用。
const initialState = {
input: '',
imageUrl: '',
box: {},
route: 'signin',
isSignedIn: false,
user: {
id: '',
name: '',
email: '',
entries: 0,
joined: ''
}
}
class App extends Component {
constructor() {
super();
this.state = initialState,
data: [],
myList: ''
}
答案 0 :(得分:1)
您可以使用spread operator syntax
添加其他值
class App extends Component {
constructor() {
super();
this.state = {
...initialState,
data: [],
myList: ''
}
}