javascript - React 上的可切换面板
问题描述
我想在 React 上制作一个可切换的面板。它由一个小面板和一个大面板组成,通过单击小面板,您可以将其替换为大面板。而且,我想在面板中包含一个元素。请参考附图。 在此处输入图像描述
这是我的代码
panel.js
import React, { useState } from "react";
import "./panel.css";
function Panel() {
const [appState,changeState] = useState({
activeObject: null,
Objects : [{id: 1},{id: 2},{id: 3},{id: 4,} ]
});
function toggleActive(index) {
changeState({...appState,activeObject: appState.Objects[index] })
}
function toggleActiveStyles(index){
if(appState.Objects[index] === appState.activeObject) {
return "box active";
} else {
return "box inactive";
}
}
return (
<div classNameq="Panel">
{ appState.Objects.map((index) => (
<div
key={index}
className={toggleActiveStyles(index)}
onClick={() => {
toggleActive(index);
}}
></div>
))}
</div>
);
}
css
.Panel {
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
flex-flow: row-reverse;
}
.box {
width: 200px;
height: 200px;
margin: 10px;
transition: transform 0.3s ease-in-out;
}
.inactive{
background-color: #3498db;
}
.active{
transform: translate(-22.4%,0)scale(0.33);
background-color: #c0392b;
}
我发现了一些类似于我想用 vue.js 编写的代码,但我无法将其翻译成 React。我会附上链接。 https://github.com/SimonZhangITer/DataVisualization/blob/master/src/components/dashboard/dashboard.vue
https://simonzhgiter.github.io/DataVisualization/#/dashboard
请帮我。
解决方案
推荐阅读
- report - 有没有办法在 tablix 和页面末尾之间显示一个字符串?
- r - ggplot:更改单个图中条形的顺序
- typescript - 如何在 Typescript 中为对象属性动态分配值?
- amazon-web-services - 如何在最佳实践中查询 DynamoDB 中的大数据
- reactjs - 我将如何创建反应应用程序,但此错误不断出现
- visual-studio - 适用于 VS 2019 的 .Net 核心模板
- python - 想把一个字符串分成三部分
- iis - 为什么设置服务器变量时 IIS 重写规则返回错误?
- javascript - 带有 Rollup 的 Vue 2.6:按变量名导入组件
- python - 脚本失败时记录时间和错误的python函数