Skip to content

Instantly share code, notes, and snippets.

@fedek6
Created May 13, 2022 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedek6/0e0a69dc2db8248c36f0e769fb5f73e8 to your computer and use it in GitHub Desktop.
Save fedek6/0e0a69dc2db8248c36f0e769fb5f73e8 to your computer and use it in GitHub Desktop.
React props naming conventions

Props naming

  • Array – use plural nouns. e.g. items
  • Number – use prefix num or postfix count, index etc that can imply a number. e.g. numItems, itemCount, itemIndex
  • Bool – use prefix is, can, has
    • is: for visual/behavior variations. e.g. isVisible, isEnable, isActive
    • can: fore behavior variations or conditional visual variations. e.g. canToggle, canExpand, canHaveCancelButton
    • has: for toggling UI elements. e.g. hasCancelButton, hasHeader
  • Object – use noun. e.g. item
  • Node – use prefix node. containerNode
  • Element – use prefix element. hoverElement

Some rules

Props describe what a component does, not why it does.

hasSubmitPermission > hasSubmitButton

isMobileScreen > isCompactLayout

<MyForm hasSubmitButton={user.canSubmit} />
<MyForm isCompactLayout={browser.isMobileScreen} />

Describe parent component instead of children:

  • <MyList onItemClick={...} /> > <MyList onClick={...} />
  • areCommentsLoading > isLoadingComments
  • hasIcon > isSpacious

Event handlers

  1. Use prefix onClick
  2. Use handle prefix for methods onClick={handleClick}
  3. Avoid conflicting with the native handler methods eg. onClick > onSelect

Based on an article.

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