抽屉器的实现AppBar with Material-UI + React{不能读取属性'的零}
Implementing a Drawer & AppBar with Material-UI + React {Cannot read property 'open' of null}
所以我试图使用React
&Material-UI
创建一个应用程序,所以我设法让AppBar启动并运行,但我有一些问题让抽屉工作。
我一直得到一个error: cannot read property open of null
,并试图找出问题是什么,没有成功。我在Stack上找到了关于这个确切问题的2个帖子,他们都没有一个能够解决我的问题的答案
无法使用Appbar + Drawer (Material UI + ReactJS)
Uncaught TypeError: Cannot read property 'open'AppBar +抽屉组件(ReactJS + Material-UI)的未定义?
这是我当前的代码:
constructor() {
super();
this.handleToggle = this.handleToggle.bind(this)
this.handleClose = this.handleClose.bind(this)
this.setState = {
open: false
};
}
handleToggle = () => this.setState({open: !this.state.open});
handleClose = () => this.setState({open: false});
render() {
return <MuiThemeProvider muiTheme={getMuiTheme()}>
<AppBar
onLeftIconButtonTouchTap={this.handleToggle}
onLeftIconButtonClick={this.handleToggle}
title="How long have you been alive?"
iconClassNameRight="muidocs-icon-navigation-expand-more" />
<Drawer
docked={false}
width={200}
open={this.state.open}
onRequestChange={(open) => this.setState({open})}
>
<MenuItem onTouchTap={this.handleClose}>Menu Item 1</MenuItem>
<MenuItem onTouchTap={this.handleClose}>Menu Item 2</MenuItem>
</Drawer>
</MuiThemeProvider>
}
(代码片段不应该运行,只是显示我的相关代码)
我想也许将this
绑定到render
会有所帮助,但它没有:(
代码中的问题是您没有在构造函数中设置状态。构造函数中的this.setState
是错误的,需要用this.state
constructor() {
super();
//...other lines of codes that you may intend to write
this.state = {
open: false
};
}
this.setState()
是一个方法,你可以调用它来从组件类的任何方法中更新组件的状态,除了render()
和constructor()
相关文章:
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 看到“;未捕获的类型错误:无法读取属性'weight'未定义的“;尽管按照字面上的指示
- angularjs无法读取未定义的属性then
- 无法读取属性'材料'未定义的Three.js
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- 未捕获的类型错误:无法读取属性'状态'在react中为null
- JavaScript承诺-无法读取属性'那么'的未定义
- Javascript Float32数组抛出无法读取属性'0'即使数组定义良好,也为null
- 未捕获的类型错误:无法读取属性'中止'的未定义
- 角度推入数组给出:TypeError:无法读取属性'推'的未定义
- http/rxjs catch回调中的Angular 2重定向导致TypeError:无法读取属性'订阅'
- 角度输入模型有条件地从一个属性读取,写入另一个属性