Javascript 可能未处理的承诺拒绝未定义不是函数
我用的是React Native。我已经退房了,但我一点也听不懂 创建组件时:Javascript 可能未处理的承诺拒绝未定义不是函数,javascript,react-native,es6-promise,Javascript,React Native,Es6 Promise,我用的是React Native。我已经退房了,但我一点也听不懂 创建组件时: render(){ 常量菜单组件=( ) ... } 我得到以下错误: '可能的未处理承诺拒绝(id:0)类型错误:未定义为 不是功能(评估) “_this.OpenSlideMenu.bind(true).then(function(){}”)” this.OpenSlideMenu()在构造函数()中声明 在render()方法中声明this.drawer: render(){ 常量菜单组件=( ) 返回( t
render(){
常量菜单组件=(
)
...
}
我得到以下错误:
'可能的未处理承诺拒绝(id:0)类型错误:未定义为
不是功能(评估)
“_this.OpenSlideMenu.bind(true).then(function(){}”)”
this.OpenSlideMenu()
在构造函数()中声明
在render()方法中声明this.drawer:
render(){
常量菜单组件=(
)
返回(
this.drawer=ref}
内容={MenuComponent}
tapToClose={true}
opendrawerofset={170}
stles={drawerStyles}
panCloseMask={0.2}
closeddrawrofset={-3}
样式={drawerStyles}
tweenHandler={(比率)=>({
main:{不透明度:(2-比率)/2}
})}>
)
}
有人能解释一下我为什么会犯这个错误吗?我如何解决这个问题?两件事都错了。您正在使用.bind(true)
将一个布尔值绑定为函数的上下文
您还在函数声明中使用了.catch()
.then()
和.catch()
用于函数调用
另外,如果这是函数的初始声明,则在声明函数之前,您正在尝试将其.bind()
绑定到该函数。你需要先申报
我建议你在MDN上多读一些
下面是一个小例子,希望能帮助您理解这些原则:
class Demo extends PureComponent {
constructor( props ) {
// allow the user this in constructor
super( props );
// set default state
this.state = {
text: "Awaiting data..."
}
// only needed if used in a callback
this.method = this.method.bind( this );
}
componentDidMount() {
// calling the method that returns a promise when component mounts
this.method()
.then((res) =>{
// set state in a non-mutating way
this.setState({text: res});
});
}
// declaring a method to handle the api call
// yours will not be named method
method() {
return new Promise(
(resolve, reject) => {
// simulating network delay of 2 seconds ( lets hope not! )
setTimeout(() => {
resolve( "Data returned from promise" );
}, 2000 );
});
);
}
render() {
// destructure the text var from data
const { text } = this.state;
// return text
return (
<p>{ text }</p>
);
}
};
类演示扩展了PureComponent{
建造师(道具){
//允许用户在构造函数中执行此操作
超级(道具);
//设置默认状态
此.state={
文本:“正在等待数据…”
}
//仅在回调中使用时才需要
this.method=this.method.bind(this);
}
componentDidMount(){
//调用在装入组件时返回承诺的方法
这个方法
。然后((res)=>{
//以非变异的方式设置状态
this.setState({text:res});
});
}
//声明处理api调用的方法
//您的将不会被命名为method
方法(){
回报新的承诺(
(解决、拒绝)=>{
//模拟2秒的网络延迟(希望不是!)
设置超时(()=>{
解决(“承诺返回的数据”);
}, 2000 );
});
);
}
render(){
//从数据中解构文本变量
const{text}=this.state;
//返回文本
返回(
{text}
);
}
};
使用调试器(在所有异常上设置中断)查看发生了什么。此.OpenSlideMenu
方法是返回承诺还是已经承诺?捕获一个返回承诺的方法没有效果,您应该只捕获一次已构造的承诺。谢谢Kyle!我能问你一个问题吗?为什么我的问题被否决了?下一次提问时,我是否需要进一步了解这个问题?因此可能会很严厉。也就是说,你的问题表明你不理解问题的许多部分,你可以在寻求帮助之前做更多的研究。
render () {
const MenuComponent = (
<MenuScreen CloseSlideMenu={this.SlideMenu.CloseSlideMenu} />
)
return (
<Drawer
ref={(ref) => this.drawer = ref}
content={MenuComponent}
tapToClose={true}
openDrawerOffset={170}
stles={drawerStyles}
panCloseMask={0.2}
closedDrawerOffset={-3}
styles={drawerStyles}
tweenHandler={(ratio) => ({
main: { opacity: (2-ratio)/2 }
})}>
<GroceryScreen selectedCategory='Todos' OpenSlideMenu={this.OpenSlideMenu} />
</Drawer>
)
}
class Demo extends PureComponent {
constructor( props ) {
// allow the user this in constructor
super( props );
// set default state
this.state = {
text: "Awaiting data..."
}
// only needed if used in a callback
this.method = this.method.bind( this );
}
componentDidMount() {
// calling the method that returns a promise when component mounts
this.method()
.then((res) =>{
// set state in a non-mutating way
this.setState({text: res});
});
}
// declaring a method to handle the api call
// yours will not be named method
method() {
return new Promise(
(resolve, reject) => {
// simulating network delay of 2 seconds ( lets hope not! )
setTimeout(() => {
resolve( "Data returned from promise" );
}, 2000 );
});
);
}
render() {
// destructure the text var from data
const { text } = this.state;
// return text
return (
<p>{ text }</p>
);
}
};