React 处理元素上的 classNames

huangapple go评论61阅读模式
英文:

React handle classNames on elements

问题

我有一个React应用程序,还有一个卡片列表。每张卡片都在一个div中,并显示div的一小部分内容。当单击div时,它应该展开并显示所有内容。我可以通过向展开的div添加一个额外的类名来实现这一点。那么在React中应该如何正确实现这个功能呢?

<div className="card" onClick={handleCardClick}>
    卡片内容
</div>

关键是,我将拥有一系列的卡片,我不能仅使用useState()来处理它们。我认为应该使用类似以下方式:

e.target.classList.add("expandCard")

但我不确定这是否是React中处理这些更改的正确方式。

英文:

I have react app and also have list of cards. Every card is in div and it shows small portion of content of the div. When you click on the div it should expand and show all content. That i can do it with adding more one className to the div that is expanded. So how it is proparly to this in react.

&lt;div className=&quot;card&quot; onClick={?}&gt;
    card conten
&lt;div&gt;

The point is i will have a lits of cards and i cant handle this with useState(). I thinks i should use somthing like:

e.target.classList.add(&quot;expandCard&quot;)

but i am not sure is that the right way to react handle the changes?

答案1

得分: 0

你需要在每张卡片中保持一个展开状态,并且如果展开了就显示内容。此外,你可以根据展开状态有条件地应用不同的类。

你可以从这段代码中获取一些想法:

import "./styles.css";
import { useState } from "react";

export default function App() {
  const [cards, setCards] = useState([
    {
      id: 1,
      title: "标题 1",
      content: "内容 1"
    },
    {
      id: 2,
      title: "标题 2",
      content: "内容 2"
    },
    {
      id: 3,
      title: "标题 3",
      content: "内容 3"
    }
  ]);

  return (
    <div>
      {cards.map((card) => (
        <Card key={card.id} title={card.title}>
          {card.content}
        </Card>
      ))}
    </div>
  );
}

function Card({ children, title }) {
  const [expanded, setExpanded] = useState(false);
  return (
    <div className={`Card ${expanded ? "expandCard" : ""}`}>
      <h1>{title}</h1>
      <button onClick={() => setExpanded((prev) => !prev)}>显示内容</button>
      {expanded && <p>{children}</p>}
    </div>
  );
}

Codesandbox链接

英文:

You need to keep an expanded state in each card, and show the content if it is expanded. Also you can apply a different class conditionally if it is expanded.

You can get an idea from this code:

import &quot;./styles.css&quot;;
import { useState } from &quot;react&quot;;

export default function App() {
  const [cards, setCards] = useState([
    {
      id: 1,
      title: &quot;title 1&quot;,
      content: &quot;content 1&quot;
    },
    {
      id: 2,
      title: &quot;title 2&quot;,
      content: &quot;content 2&quot;
    },
    {
      id: 3,
      title: &quot;title 3&quot;,
      content: &quot;content 3&quot;
    }
  ]);

  return (
    &lt;div&gt;
      {cards.map((card) =&gt; (
        &lt;Card key={card.id} title={card.title}&gt;
          {card.content}
        &lt;/Card&gt;
      ))}
    &lt;/div&gt;
  );
}

function Card({ children, title }) {
  const [expanded, setExpanded] = useState(false);
  return (
    &lt;div className={`Card ${expanded ? &quot;expandCard&quot; : &quot;&quot;}`}&gt;
      &lt;h1&gt;{title}&lt;/h1&gt;
      &lt;button onClick={() =&gt; setExpanded((prev) =&gt; !prev)}&gt;Show Content&lt;/button&gt;
      {expanded &amp;&amp; &lt;p&gt;{children}&lt;/p&gt;}
    &lt;/div&gt;
  );
}

Codesandbox

huangapple
  • 本文由 发表于 2023年6月18日 22:31:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定