reactjs - 如何在另一个组件的函数中渲染反应组件?
问题描述
所以我需要渲染“警报”组件,它通知用户组件中的成功操作 - 内部函数,它将项目添加到购物车/本地存储。
报警组件:
class Alarm extends React.Component{
render(){
return(
<Alert color="success">
This is a success alert — check it out!
</Alert>
)
}
}
另一个组件中的功能:
addToCart = () => {
(Some Logic...)
}
编辑
我有 2 个组件:
- 警报(返回警报组件)
- 产品项目(包括函数 addToCart)
初始化函数 addToCard 后,我需要渲染警报组件
解决方案
我在构造函数状态中声明“x”:
constructor(props) {
super(props);
this.state = {
quantity: 1,
x: false
}
}
并添加到 ProductItem 组件:
<div>
{ this.state.x &&
<Alarm />
}
</div>
而内部函数只是:
this.setState({x: true})
解决方案
推荐阅读
- node.js - 设备未连接到同一网络中的主机
- python-3.x - django 中的 Templateview 出现错误“字段 'id' 需要一个数字,但得到了 'form_file'”
- python - 有没有办法将两个不同的 Python 文件复合到一个 Docx 文档中提取?
- antd - 是否可以在 popconfirm 中放置一个单选按钮,并且仅当用户单击 popconfirm 上的确认时才检查单选按钮?
- javascript - 从 fs.readFile 获取不准确的结果
- docker - docker运行报错:layer不存在
- javascript - 语法错误:使用“fast-image-zoom”时无法在模块外使用导入语句
- c# - 如何在调试时找出 wcf 检查的所有端点
- flutter - 无法打开设置文件的设置通用类缓存
- python - 使用 pandas 将 Json 转换为 DataFrame