Preact/X/Props Children To Child Array

/icons/calendar.svg

Last update

Aug 21, 2024

A codemod that enhances child handling in Preact components by using toChildArray for accurate child count.

Detailed description:
This codemod refactors Preact components to improve the handling of props.children. It introduces the toChildArray utility from Preact, ensuring that child elements are accurately counted, especially when dealing with fragments or nested arrays. This leads to more robust and predictable behavior in your components.

Example

Before

function Foo(props) {
const count = props.children.length;
return < div > I have { count } children < /div>;
}

After

import { toChildArray } from 'preact';
function Foo(props) {
const count = toChildArray(props.children).length;
return < div > I have { count } children < /div>;
}

Build custom codemods

Use AI-powered codemod studio and automate undifferentiated tasks for yourself, colleagues or the community

background illustrationGet Started Now