reactjs - 组件返回白色空白屏幕
问题描述
我正在尝试检查用户是否授权使用相机和位置,如果他没有,那么应该渲染一个简单的屏幕让他知道。该函数被调用,但返回语句返回空白屏幕而不是组件。
注意:我尝试使用 'backgroundColor: 'black' 来查看是否渲染,我什至看不到黑色背景。
代码:
componentWillMount() {
Permissions.checkMultiple(['camera', 'location']).then(response => {
//response is an object mapping type to permission
console.log('permission check')
console.log('response.camera', response.camera)
console.log('response.location', response.location)
if (response.camera === 'denied' || response.location === 'denied') {
return (
<View>
<Text>
Sorry you cant use this app without allowing Location and Camera permmision
to do it just go to Setting/Keepr and allow Location and Camera access for this app
</Text>
<Button title={'go to settings'} onPress={Permissions.openSettings}></Button>
</View>
)
}
})
}
解决方案
您正在尝试在componentWillMount
... 和 Promise 回调中返回 JSX。这根本行不通,因为您需要从render
方法返回 JSX。你可以使用 react-state 来做到这一点。例子:
class MyComponent extends React.Component {
constructor(props) {
this.state = {
response: {}
};
}
componentWillMount() {
Permissions.checkMultiple(["camera", "location"]).then(response => {
this.setState({ response });
});
}
render() {
const { response } = this.state;
if (response.camera === "denied" || response.location === "denied") {
return (
<View>
<Text>
Sorry you cant use this app without allowing Location and Camera
permmision to do it just go to Setting/Keepr and allow Location and
Camera access for this app
</Text>
<Button title={"go to settings"} onPress={Permissions.openSettings} />
</View>
);
}
return <p>something</p>;
}
}
推荐阅读
- python - 为什么一行中的多个列表值分配不会改变列表中的相应值?
- c# - Blazor 使元素在 x 秒后消失
- javascript - Node.js Express.js Mongoose 错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头
- hyperledger-fabric - 将结构从 1.4.4 升级到 2.2 时出现问题
- django - 在生产中在 Django 3 中实现自定义 404 页面的正确方法
- flutter - 如何从烧瓶 API 接收 http.MultipartRequest 响应中的图像文件?
- reactjs - 将firebase通知定向到reactjs应用程序内部
- python - 按日期范围在数据框中创建新行
- c# - azure AD 中的范围与应用程序中的范围不同(Microsoft Graph Api)
- c++ - SetWindowsHookEx 使任务栏打乱,而“新闻和兴趣”已关闭