javascript - 无法将来自套接字 io 的数据作为道具传递给反应组件
问题描述
我尝试了几种方法,但似乎没有任何效果:(我试图通过构建一个实现 Socket io 的简单应用程序来学习 React,但找不到将“响应”作为道具传递给子组件的方法。
import socketIOClient from "socket.io-client";
import Main from "./Main";
const ENDPOINT = "http://127.0.0.1:4001";
function App() {
const [response, setResponse] = useState([]);
useEffect(() => {
const socket = socketIOClient(ENDPOINT);
socket.on("tick", (data) => {
setResponse(data);
});
}, []);
console.log(response);
return (
<>
<div style={{ textAlign: "center" }}>
<Main
responses = {response}
/>
</div>
</>
);
}
export default App;
import React from 'react';
import './Main.css';
class Main extends React.Component {
render() {
return (
<div className="container">
<div className="placeholder">hello {this.props.responses}</div>
<div className="placeholder"></div>
<div className="placeholder"></div>
</div>
);
}
}
export default Main;
我收到这个错误
× × 错误:对象作为 React 子对象无效(找到:对象与键 {currencyPairName, prices})。如果您打算渲染一组子项,请改用数组。▶ 22 个堆栈帧被折叠。插座。src/App.js:14
11 | useEffect(() => {
12 | const socket = socketIOClient(ENDPOINT);
13 | socket.on("tick", (data) => {
> 14 | setResponse(data);
| ^ 15 | });
16 | }, []);
17 |
我的响应包含一个对象数组
希望有人能告诉我光..
解决方案
推荐阅读
- google-sheets - 如何按类别统计条目?
- database - 使用 laravel 为不同的 API 制作单个端点
- reinforcement-learning - 更新 W 错过理解时的离策略蒙特卡罗方法
- reactjs - 我们选择了一个复选框,但是当我们取消表单时应该自动取消选中它......这在 react js 中怎么可能?
- python - 使用 Python 以 4 的间隔打印 0 到 999 之间的数字
- popupwindow - 如果我们单击 powerbuilder 中的按钮,如何弹出带有一些 I/O 字段的窗口
- python - 我们如何使用公式将工作表添加到 Excel 工作簿?
- java - 如何在 Apache POI 中设置条件格式规则的优先级?
- javascript - 在 HTML 中创建 Ludo 应用程序 || 无法在用户之间切换机会
- java - Spring Boot WebServiceTemplate 编码问题