在React中以有效的方式显示相同风格的手风琴式不同内容?

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

Effective way to display different contents in the same style of accordion in React?

问题

以下是您要翻译的内容:

什么是在多个手风琴中显示不同内容的有效方法?
输入图像描述
这是我在我的项目中正在做的事情,显然,运输信息和交付选项将包含不同的文本框、标签和输入框。

到目前为止,我正在为不同的手风琴硬编码每个标签、输入框和文本框,但我认为这不是很聪明和有效的做法。硬编码使我的代码在这一页变得非常长。以下是我如何硬编码所有内容的代码片段。
或者,我应该简单地将不同的手风琴分成不同的React类,以使代码更短。

<div className="dropdown-header" onClick={toggle}>
    <h4 className="dropdown-text">Shipping Information</h4>
    <span className="dropdown-selection">{selected ? '-' : '+'}</span>
</div>
{selected && (
    <div className="wrapper">
        <div className="row">
            <div className="col-md-6">
                <div className="info-group mb-3">
                    <label>First name</label>
                    <input type="text" className="info-input" name="firstname" />
                </div>
            </div>
            <div className="col-md-6">
                <div className="info-group mb-3">
                    <label>Last name</label>
                    <input type="text" className="info-input" name="firstname" />
                </div>
            </div>
        </div>
    </div>
)}

请注意,代码部分未进行翻译。

英文:

what is an effective way to display different contents in the multiple accordions?
enter image description here
This is what I am doing in my project, and obviously shipping information and delivery options would contain different textboxs, labels, and input boxes.

As of now, I am hardcoding every label, input box, textbox for the different accordions, but I don't think this is very smart and effective. Hardcoing makes my code very long for this one page. Below is a snippet of how I am hardcoding everything.
Or should I simply divide the different accordions into different react classes to make the code shorter.

&lt;div className=&quot;dropdown-header&quot; onClick={toggle}&gt;
                                &lt;h4 className=&quot;dropdown-text&quot;&gt;Shipping Information&lt;/h4&gt;
                                &lt;span className=&quot;dropdown-selection&quot;&gt;{selected ? &#39;-&#39; : &#39;+&#39;}&lt;/span&gt;
                            &lt;/div&gt;
                            {selected &amp;&amp; (&lt;div className=&quot;wrapper&quot;&gt;
                                &lt;div className=&quot;row&quot;&gt;
                                    &lt;div className=&quot;col-md-6&quot;&gt;
                                        &lt;div className=&quot;info-group mb-3&quot;&gt;
                                            &lt;label&gt;First name&lt;/label&gt;
                                            &lt;input type=&quot;text&quot; className=&quot;info-input&quot; name=&quot;firstname&quot; /&gt;
                                        &lt;/div&gt;
                                    &lt;/div&gt;
                                    &lt;div className=&quot;col-md-6&quot;&gt;
                                        &lt;div className=&quot;info-group mb-3&quot;&gt;
                                            &lt;label&gt;Last name&lt;/label&gt;
                                            &lt;input type=&quot;text&quot; className=&quot;info-input&quot; name=&quot;firstname&quot; /&gt;
                                        &lt;/div&gt;

答案1

得分: 0

你需要将所有需要重复使用的部分制作成一个组件,并通过参数传递你想要不同的内容。

请参考:https://react.dev/learn/your-first-component

例如:

export function AccordionExample({ heading, label1, label2 }) {
 return (
  <div className="dropdown-header" onClick={toggle}>
    <h4 className="dropdown-text">{heading}</h4>
    <span className="dropdown-selection">{selected ? '-' : '+'}</span>
  </div>
  {selected && (
    <div className="wrapper">
      <div className="row">
        <div className="col-md-6">
          <div className="info-group mb-3">
            <label>{label1}</label>
            <input type="text" className="info-input" name="firstname" />
          </div>
        </div>
        <div className="col-md-6">
          <div className="info-group mb-3">
            <label>{label2}</label>
            <input type="text" className="info-input" name="lastname" />
          </div>
        </div>
      </div>
    </div>
  )}
 );
}

然后,你可以在需要的地方调用你的组件,如下所示:

<AccordionExample heading="Shipping Information" label1="First Name" label2="Last Name" />
<AccordionExample heading="Delivery Option" label1="Option One" label2="Option Two" />
英文:

You need to make everything that's going to be re-used into a component itself and pass through parameters you want to be different.

Please see: https://react.dev/learn/your-first-component

For Example:

export function AccordionExample({heading, label1, label2}) {
 return (
  &lt;div className=&quot;dropdown-header&quot; onClick={toggle}&gt;
    &lt;h4 className=&quot;dropdown-text&quot;&gt;{heading}&lt;/h4&gt;
    &lt;span className=&quot;dropdown-selection&quot;&gt;{selected ? &#39;-&#39; : &#39;+&#39;}&lt;/span&gt;
  &lt;/div&gt;
  {selected &amp;&amp; (&lt;div className=&quot;wrapper&quot;&gt;
    &lt;div className=&quot;row&quot;&gt;
      &lt;div className=&quot;col-md-6&quot;&gt;
        &lt;div className=&quot;info-group mb-3&quot;&gt;
          &lt;label&gt;{label1}&lt;/label&gt;
          &lt;input type=&quot;text&quot; className=&quot;info-input&quot; name=&quot;firstname&quot; /&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div className=&quot;col-md-6&quot;&gt;
        &lt;div className=&quot;info-group mb-3&quot;&gt;
          &lt;label&gt;{label2}&lt;/label&gt;
          &lt;input type=&quot;text&quot; className=&quot;info-input&quot; name=&quot;lastname&quot; /&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  } 
  );
}

You would then call your component where needed like so:

&lt;AccordionExample heading=&quot;Shipping Information&quot; label1=&quot;First Name&quot; label2=&quot;Last Name&quot; /&gt;
&lt;AccordionExample heading=&quot;Delivery Option&quot; label1=&quot;Option One&quot; label2=&quot;Option Two&quot; /&gt;

huangapple
  • 本文由 发表于 2023年6月8日 13:57:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428967.html
匿名

发表评论

匿名网友

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

确定