I have a component in React that can take an indefinite number of children, and it uses those children to generate another component, with the original children composed within it. The point is to wrap the original child component with a new component that decorates the functionality of it. The code looks like this:
const Form = ({ children }) => (
<FormGroupComponent>
{children.map(child => (
<CheckboxAndChild
checked={child.props.checked}
setCheckbox={child.props.setCheckbox}
>
{child}
</CheckboxAndPicker>
))}
</FormGroupComponent>
);
const CheckboxAndPicker = ({ checked, setCheckbox, children }) => (
<div>
<div>
<Checkbox
checked={checked}
onChange={() => setCheckbox(!checked)}
/>
</div>
<div>{checked && children}</div>
<div/>
</div>
);
These two functional components can be used like this:
...
<Form>
<TimePicker
{...timePickerProps}
checked={showTimeOne}
setChecked={setTimeOne}
/>
<TimePicker
{...timePickerProps}
checked={showTimeTwo}
setChecked={setTimeTwo}
/>
</Form>
...
I find two things strange about this code:
- The "TimePicker" component doesn't normally have a "checked" and "setChecked" prop within it, and doesn't do anything with those props itself. Those props are only used to render the "CheckboxAndPicker" component within the "Form" component.
- The "Form" component accesses its children component's props in order to generate a new component using those props.
Is this an anti-pattern, or is this sort of functionality acceptable when decorating existing components with new props, and using those new props to generate new components?