reactjs - 如何在反应js中附加标签和样式,跨度方法
问题描述
每当调用套接字方法时,我想附加一个带有样式的标签和跨度方法,请帮助我提前感谢
socket.on('start_call', async (customerName, Id) =>{
// here i want to append the customerName for label and Id for span
}
解决方案
我认为,您应该以这种方式应用(只是想法和简化版本,如果您在与代码集成时遇到任何困难,请告诉我)。
import React, { useState } from 'react';
const App = () => {
const [customers, setCustomers] = useState([]);
useEffect(() => {
socket.on('start_call', async (name, id) => {
setCustomers([...customers, { name, id }])
}
}, []) // Run once when component render, the same as `componentDidMount` on class base component
return (
<div>
{customers.length > 0 && customers.map((customer) => {
<label>{customer.name}</label>
<span>{customer.id}</span>
})}
</div>
)
}
export default App;
推荐阅读
- c# - 在 WPF 中,如何使用路径标记语法绘制复合几何?
- webpack - 为什么 workbox-webpack-plugin 默认会排除名为 manifest*.js(on) 的文件?
- angularjs - 离子无限滚动反向
- apache-spark-sql - spark-dataframe 透视缺失的列/值
- c# - 在 Controler Asp net .core 中添加文章
- api - UrlFetchApp.fetch() 的深入调试?返回 - '地址不可用'
- angular - 使用模板驱动的 Angular 6 中创建和编辑数据的相同表单
- javascript - 异步获取错误后隐藏模式不起作用
- php - 在雄辩的多对多插入中添加多个额外属性数组给了我错误
- c++ - 用于对齐 QLabel 上的非恒定宽度文本的通用代码