reactjs - 为什么我的 React 组件没有显示或根本没有动作?
问题描述
如果有人试图在移动浏览器上查看我的网页,我想在屏幕上弹出一条通知,告诉他们这是不可能的。我最初尝试将其硬编码到App.js
类中,但决定使用一个组件。但是,当我尝试查看是否转到移动设备(通过右键单击 + 检查元素提供的 Edge 仿真)时,没有显示任何通知。
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { render } from 'react-dom'
import { Provider as AlertProvider } from 'react-alert'
import AlertTemplate from 'react-alert-template-basic'
import { useAlert } from 'react-alert'
import { isMobile } from 'react-device-detect';
import { Alert } from 'reactstrap';
import { UncontrolledAlert } from 'reactstrap';
class Disclaimer extends Component {
constructor(props) {
super(props);
this.state = {
visible: true
};
this.onDismiss = this.onDismiss.bind(this);
}
onDismiss() {
this.setState({ visible: false });
}
render(){
return(
<Alert color="danger" isOpen={this.state.visible} toggle={this.onDismiss}>
This webpage is not optimized for mobile.
</Alert>
);
}
}
}
export default Disclaimer;
我最终调用这个组件在 App.js 中返回,但我看不到它。但是,当我查看“元素”时,我确实在层次结构中看到了它,并且屏幕上有一个位置给出了轮廓,但就警报应该做什么而言,没有任何可见的东西。我确实有一个用于警报的 .css 文件,但它只是font-family
和.css 文件text-align
。
更新:我也试过在 App.jsrender()
方法中这样做:
<MobileView>
<button
onLoad={() => {
if(isMobile){
alert.show('This page is not optimized for mobile!')
}
}}
>
Show Alert
</button>
</MobileView>
我将它从按钮更改为警报,并且看不到任何内容。话虽如此,当我使用浏览器,模拟移动设备,然后检查元素时,我可以转到元素和样式,我可以取消选中具有“不透明度 0”的框或编辑该值。假设我将按钮更改为警报,我该如何在代码中执行此操作???
解决方案
推荐阅读
- java - 是否可以使用 setWeightSum(float) 以编程方式更改 weightSum 值?
- python - 函数调整图像大小:(-215:断言失败)!ssize.empty()在函数“resize”中
- c# - 如何根据 CheckBox 值(MVVM)过滤集合
- java - JNI:从 C++ 设置 jobjectArray
- javascript - React Native - 为什么我的条件 If/If Else 语句永远不会通过?
- javascript - 经典 ASP 查询字符串变量已设置和未设置
- oracle - 无法从 Glassfish 池连接到外部 Oracle 数据库,但我可以从 DBeaver
- python - Django Order By on Reverse Foreign Key value 删除没有关系的项目
- python - 如何将数据从屏幕存储到屏幕?
- firefox - Spring Fox (Swagger-UI) 2.9.2 如何禁用“试用”按钮