简体   繁体   English

通过 function 为多个项目生成 react.fragment

[英]Generate react.fragment for multiple items via function

In the midst of me cleaning out a massive react render statement, I've been breaking some things out into helper functions.在我清理大量 React 渲染语句的过程中,我一直在将一些东西分解成辅助函数。 Here's a portion of what I have in a helper function:这是我在助手 function 中的一部分:

// This looks ugly
return (
  <>
    {label}
    {getDropdown(
      daysOfWeek,
      handleDaysOfWeekChange,
      daysOfWeekLabel,
      options.dayOfWeek,
      isMultiSelectEnabled
    )}
  </>
);

The particulars of the getDropdown function don't matter. getDropdown function 的细节无关紧要。 Label is a react component (and is defined as a variable since it's used elsewhere as well), and getDropdown returns a react component. Label 是一个反应组件(并且被定义为一个变量,因为它也在其他地方使用),并且 getDropdown 返回一个反应组件。

For some reason this just looks ugly to me, we start with a react.fragment so that we can have multiple javascript code snippets, but in turn each javascript snippet is either a variable which equals a react component, or a function which is computed at runtime to a react component.出于某种原因,这对我来说看起来很丑陋,我们从一个 react.fragment 开始,这样我们就可以有多个 javascript 代码片段,但反过来每个 javascript 片段要么是一个等于反应组件的变量,要么是一个 function 计算在反应组件的运行时。 So we're really going from the react paradigm to javascript paradigm and then back to react.所以我们真的是从反应范式到 javascript 范式,然后再回到反应范式。

I'd like something like this which is all javascript paradigm:我想要这样的东西,它全是 javascript 范例:

// invalid code!
return React.fillFragment(
  label,
  getDropdown(
    daysOfMonth,
    handleDaysOfMonthChange,
    daysOfMonthLabel,
    options.dayOfMonth,
    isMultiSelectEnabled
  ),
);

Returning an array just gives key errors, and we wouldn't want to overload this since there are other areas where we would want to return an array without a fragment.返回一个数组只会给出关键错误,我们不想重载它,因为在其他地方我们想要返回一个没有片段的数组。

The next best solution that I can think of is to simply treat these two as actual components, which would be the all react paradigm:我能想到的下一个最佳解决方案是将这两个简单地视为实际组件,这将是 all react 范例:

// Works, but still feels verbose
return (
  <>
    <Label/>
    <GetDropDown
      value={daysOfMonth}
      onChange={handleDaysOfMonthChange}
      defaultLabel={daysOfMonthLabel}
      listOptions={options.dayOfMonth}
      isMultiSelect={isMultiSelectEnabled}
    />
  </>
);

And this does work, but it feels a little overkill and isn't the most succinct for a couple one time use functions.这确实有效,但感觉有点矫枉过正,而且对于一对一次性使用的函数来说并不是最简洁的。 (for example label is only used here and one other spot.) (例如 label 仅在此处和其他地方使用。)

I'm wondering is there something built in that I haven't been able to find, or is there a little trick here that I've never come across?我想知道是否内置了一些我无法找到的东西,或者这里是否有我从未遇到过的小技巧? Or maybe I'm being too picky或者也许我太挑剔了

You last snippet look best to me.你最后的片段对我来说最好看。

If getDropdown returns a React component, then it is probably best for it to be a React component.如果getDropdown返回一个 React 组件,那么它最好是一个 React 组件。

If you dislike <></> fragment syntax, you can put <Fragment></Fragment> instead.如果你不喜欢<></>片段语法,你可以用<Fragment></Fragment>代替。

If you want no fragment at all, you either have to inline your components Label and GetDropDown into their parent component.如果您根本不需要片段,则必须将组件LabelGetDropDown内联到它们的父组件中。 Or put Label into GetDropDown ( GetDropDown should be renamed Dropdown by the way).或者将Label放入GetDropDownGetDropDown改名为Dropdown )。

I don't know the content of GetDropDown and Label , but if there is nothing more than in a select and label , you may not want to create a React component.我不知道GetDropDownLabel的内容,但是如果除了selectlabel中没有其他内容,您可能不想创建 React 组件。

If you have custom style applied to the label and select element, you can either add a global style sheet, or -preferred solution- use a css-in-js library like styled-components :如果您将自定义样式应用于labelselect元素,您可以添加全局样式表,或者 - 首选解决方案 - 使用 css-in-js 库,如styled-components

// Label is a component, with the custom style you define here.
const Label = styled.label`
  color: grey;
`;

Adding small components like that, has negligible performance cost.添加这样的小组件,性能成本可以忽略不计。

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

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