Function withBody

  • withBody can be used as an intermediator to convert props and pass to the component

    Type Parameters

    • Props extends object

    • Ret extends object

    Parameters

    Returns Hoc<[WithBodyFn<ToSchema<Props>, ToSchema<Ret>>]>

    Example

    // in this example, HappinessByLevel logs according to level 1 to 5
    function HappinessByLevel({ level }: { level: number }) {
    if (level >= 5) {
    return "Very Happy";
    }
    if (level === 4) {
    return "Happy";
    }
    if (level === 3) {
    return "Neutral";
    }
    if (level === 2) {
    return "Sad";
    }
    // very sad if level is 1 or less
    return "Very Sad";
    }

    const HappinessByGradient = withBody(function convertToGradient({
    level,
    }: {
    level: number;
    }) {
    // convert a value from 0 to 1 to a gradient level
    return { level: Math.ceil(level * 5) };
    })(HappinessByLevel);

    Example

    const AddOne = withBody(function acceptStringToo({
    value,
    }: {
    value: number | string;
    }) {
    return { value: typeof value === "string" ? parseInt(value, 10) : value };
    })(function AddOne({ value }: { value: number }) {
    return <>{value + 1}</>;
    });

Generated using TypeDoc