View on GitHub

docs-as-code-samples

A repository with docs-as-code and markdown samples

This document covers the preferred method for removing defaultProps (deprecated with React v19) from frontend components, to eliminate the numerous console errors re: deprecation warning.

Many defaultProps in components are unnecessary and only supply sample data values for Storybook.
This isn’t always true, so each component has to be evaluated.

Method 1: Delete the defaultProps Section

Easiest method, works for most components.

Use this method when:

Steps:

  1. Delete the entire defaultProps section without any other modification to the component.

Examples:

sampleTagComponent component at src/components/sampleTagComponent.jsx

sampleTagComponent.defaultProps = { tagCategories: [ { href: '', value: 'Tag 1', }, { href: '', value: 'Longer Tag 2', }, { href: '', value: 'A very long Tag 3', }, ], }

sampleCardComponent component at src/components/sampleCardComponent.jsx

sampleCardComponent.defaultProps = { resultCardCode: '', allTrips: { image: '', title: '', distanceAndTime: '', totalDuration: '1' + '-' + '2', destination: { start: '', end: '', }, tripDesc: '', urlLink: '', stops: [], langcode: 'en', }, }

sampleCTAComponent component at src/components/sampleCTAComponent.jsx

sampleCTAComponent.defaultProps = { background: '', hue: 'light', styleClass: 'default', closeIcon: false, }

Method 2: Delete the defaultProps Section and Update Instances of the Component

Use this method when:

Steps:

  1. Delete the entire defaultProps section without any other modification to the component.
  2. Locate and update instances of the component that need these settings.

Examples:

sampleVideoPlayerComponent at src/components/sampleVideoPlayerComponent.jsx

sampleVideoPlayerComponent.defaultProps = { url: '', light: false, width: '100%', height: '100%', title: '', controls: true, playing: false, loop: false, muted: false, datafy: 'datafy-event-youtube-video-click', autoload: false, } Add the props in the implementation:

`{allNativeVideos.map((video, index) => ( <sampleVideoPlayerComponent key={video.name} url={video.field_media_oembed_video} width={isMobile ? ‘325px’ : ‘582px’} height={isMobile ? ‘182px’ : ‘327px’} title={video.name}


ADD PROPS HERE FOR datafy, controls, playing, loop, muted


/> ))}`

Method 3: Use Javascript Parameters

Use this method when:

Steps:

  1. Delete the entire defaultProps section.
  2. Refactor the component.

When refactoring the component, the only props that should be added as parameters are those that supply necessary values. defaultProps with empty values, no-op functions, empty arrays or stub data do NOT need to be added as parameters.

Examples:

sampleCardGroupComponent component at src/components/sampleCardGroupComponent.jsx

sampleCardGroupComponent.defaultProps = { breakpointColumnsObj: { default: 3, 948: 2, 500: 1, }, } Original component:

`const sampleCardGroupComponent = (props) => { return ( <Masonry breakpointCols={props.breakpointColumnsObj} className=”m-sampleCardGroupComponent” columnClassName=”m-sampleCardGroupComponent__col”

{props.children} </Masonry> ) }` Becomes:

`const sampleCardGroupComponent = ({ breakpointColumnsObj = { default: 3, 948: 2, 500: 1 }, children, }) => { return ( <Masonry breakpointCols={breakpointColumnsObj} className=”m-sampleCardGroupComponent” columnClassName=”m-sampleCardGroupComponent__col”

{children} </Masonry> ) }`