简体   繁体   English

如何使用 React 循环可重用组件

[英]how to loop a reusable component using react

I am working with react, and I came across one thing, which is looping through component我正在使用 React,我遇到了一件事,那就是遍历组件

  • I have one inout field component which is generic.我有一个通用的 inout 字段组件。
  • when I have to create two input fields I am defining one data and looping the input component当我必须创建两个输入字段时,我正在定义一个数据并循环输入组件

My doubt我的疑惑

My doubt is shall I loop the input component where I am calling it, like below我的疑问是我是否应该在调用它的地方循环输入组件,如下所示

{

inputData.map((li,ind)=>{
  return(
    <InputComp
    key={ind}
    type={li.type}
    name={li.name}
    id={li.id}
    className={li.className}
    />
  )
})}

Or shall I pass data as props and loop the inout field like below或者我应该将数据作为道具传递并循环 inout 字段,如下所示

<InputComp data={data}/> // calling the component

{data.map((li,ind)=>{ // inside component looping
  <input
  key={ind}
  type={li.type}
  name={li.name}
  className={li.className}
  />
})}

I am bit confused which is best over the other, my requirement can be of many types one is like above to create a normal input field the other can be to create dynamic input field I can have one button on click of which I have to add one new row and one more button to delete that row (row means input field)我有点困惑哪个最好,我的要求可以有多种类型,一种是像上面那样创建一个普通输入字段,另一种可以是创建动态输入字段我可以点击一个按钮,我必须添加一个新行和一个删除该行的按钮(行表示输入字段)

SO I am writing this for better understanding for code, if there is one more option to create a input component I am open to use that code.所以我写这篇文章是为了更好地理解代码,如果还有一个创建输入组件的选项,我愿意使用该代码。

I'd choose the first approach for following reasons:我会选择第一种方法,原因如下:

  • You don't make another (probably single use) component just for rendering multiple input.您不会仅仅为了呈现多个输入而制作另一个(可能是一次性使用的)组件。
  • You are passing data as keyed props instead of the entire object, which makes it easier to debug and gives you more control over that component.您将数据作为键控道具而不是整个 object 传递,这使得调试更容易,并使您可以更好地控制该组件。
  • It makes your code stricter, which is better for having less mess in it.它使您的代码更严格,这样可以减少代码中的混乱。

Also remind you, that if you .map() elements, you should add key attribute to it.还要提醒你,如果你有.map()元素,你应该给它添加key属性。

As follow up for comment:作为后续评论:

Because you essentially have one extra component, that could contain an error.因为您基本上有一个额外的组件,所以它可能包含一个错误。 It's not insane difference, but it's something to mention.这不是疯狂的区别,但值得一提。 Mostly it would come down to better control over it and flexibility of that component, because if you were to have condition for something in data, you could have it in that map instead of putting it into the <InputComp/> and making it less flexible.大多数情况下,它会归结为更好地控制它和该组件的灵活性,因为如果你对数据中的某些东西有条件,你可以在那个map中拥有它,而不是将它放入<InputComp/>并降低它的灵活性.

Example of this would be:这方面的例子是:

// This is only mockup
data.map((item) => {
  if(item.name === "dog"){
    return <Input .../>
  }
}

Would mean that if you passed data you would do:意味着如果你传递数据你会做:

// This is only mockup
const Input = (props) => {
  if(props.data.name){
    return <input />
  }
  return <>
}

But what are you going to do, when you don't want to use it on dogs?但是,当您不想在狗身上使用它时,您打算怎么办? Make another component?制作另一个组件? Pass controlling parameter?传递控制参数? Remap data?重新映射数据? At that point you can map inputs.那时你可以输入 map。 It will bubble the code up for no reason.它会无缘无故地冒泡代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM