{CC:{totalWorkstaion:100,correctWorkstaion:30,seriousWorkstaion:10,normalWorkstaion:20} CK: {totalWorkstaion:100,correctWorkstaion:30,seriousWorkstaion:10,normalWorkstaion:20} SB: {totalWorkstaion:100,correctWorkstaion:30,seriousWorkstaion:10,normalWorkstaion:20} SS:{totalWorkstaion:100,correctWorkstaion:30,seriousWorkstaion:18,normalWorkstaion:20} SV:{totalWorkstaion:100,correctWorkstaion:30,seriousWorkstaion:10,normalWorkstaion:20}}使用react函数组件完成以上数据通过点击CC,CK,SB,SS,SV的数据切换
时间: 2024-03-20 10:45:28 浏览: 119
Node.js: JavaScript based framework. Easy Guide Book
4星 · 用户满意度95%
好的,这是一个比较具体的问题,我会尽力回答。首先,您需要使用React函数组件创建一个组件,该组件将渲染一些数据。然后,您需要在该组件中创建一些按钮,每个按钮对应于您提供的五个对象之一。每当用户点击其中一个按钮时,组件应该更新其状态,以便渲染相应的数据。
下面是一个可能的解决方案的代码:
```jsx
import React, { useState } from "react";
const data = {
CC: { totalWorkstaion: 100, correctWorkstaion: 30, seriousWorkstaion: 10, normalWorkstaion: 20 },
CK: { totalWorkstaion: 100, correctWorkstaion: 30, seriousWorkstaion: 10, normalWorkstaion: 20 },
SB: { totalWorkstaion: 100, correctWorkstaion: 30, seriousWorkstaion: 10, normalWorkstaion: 20 },
SS: { totalWorkstaion: 100, correctWorkstaion: 30, seriousWorkstaion: 18, normalWorkstaion: 20 },
SV: { totalWorkstaion: 100, correctWorkstaion: 30, seriousWorkstaion: 10, normalWorkstaion: 20 }
};
function App() {
const [selectedData, setSelectedData] = useState(data.CC);
const handleButtonClick = (key) => {
setSelectedData(data[key]);
};
return (
<div>
<div>
<button onClick={() => handleButtonClick("CC")}>CC</button>
<button onClick={() => handleButtonClick("CK")}>CK</button>
<button onClick={() => handleButtonClick("SB")}>SB</button>
<button onClick={() => handleButtonClick("SS")}>SS</button>
<button onClick={() => handleButtonClick("SV")}>SV</button>
</div>
<div>
<p>Total Workstation: {selectedData.totalWorkstaion}</p>
<p>Correct Workstation: {selectedData.correctWorkstaion}</p>
<p>Serious Workstation: {selectedData.seriousWorkstaion}</p>
<p>Normal Workstation: {selectedData.normalWorkstaion}</p>
</div>
</div>
);
}
export default App;
```
在此代码中,我们首先导入React和useState钩子函数。然后,我们定义了一个名为“data”的对象,其中包含您提供的五个对象。接下来,我们创建了一个名为“App”的函数组件,并定义了一个名为“selectedData”的状态变量,它的初始值为CC对象。然后,我们定义了一个名为“handleButtonClick”的函数,它接受一个键作为参数,并根据该键来更新“selectedData”的状态。最后,我们在组件的返回值中创建了一些按钮,每个按钮都与“handleButtonClick”函数相关联,并且还渲染了一些文本,以显示“selectedData”的当前值。
阅读全文