Skip to content

Instantly share code, notes, and snippets.

@mrcoles
Created July 26, 2019 18:51
Show Gist options
  • Save mrcoles/ebd34c3a3ca39614c8ad250e5f83e9be to your computer and use it in GitHub Desktop.
Save mrcoles/ebd34c3a3ca39614c8ad250e5f83e9be to your computer and use it in GitHub Desktop.
A simple React component for handling pluralization of a word.
import React from 'react';
export const Plural = ({
num,
text,
pluralText,
pluralSuffix,
singularSuffix
}: {
num: number;
text: string;
pluralText?: string;
pluralSuffix?: string;
singularSuffix?: string;
}) => {
return (
<>
{num === 1
? text + (singularSuffix || '')
: pluralText
? pluralText
: text + (pluralSuffix || 's')}
</>
);
};
@mrcoles
Copy link
Author

mrcoles commented Jul 26, 2019

Usage:

const WhatTheDogsSee: React.FunctionComponent<{
  numDogs: number;
  numGeese: number;
}> = ({ numDogs, numGeese }) => (
  <p>
    The {numDogs} <Plural text="dog" num={numDogs} />{' '}
    <Plural
      text="watch"
      singularSuffix="es"
      pluralSuffix=""
      num={numDogs}
    />{' '}
    the
    {numGeese} <Plural text="goose" pluralText="geese" num={numGeese} />{' '}
    fly.
  </p>
);

πŸ¦† πŸ‘€ πŸ•πŸ•

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment