Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Why doesn't default props work in this case?

I am training how to use default props and I ran into a problem. Can anyone explain to me why default props doesn't work in this case?

import React from 'react';
import PropTypes from 'prop-types';
import defaultImage from '../../../assets/DefaultImageOnError.svg'

export const ImageComponent = ({ props }) => {
    return (
        <img width={ props.size } src={ props.src } alt={ props.alt }/>
    );
};

ImageComponent.propTypes = {
    props: PropTypes.objectOf(PropTypes.string),
};

ImageComponent.defaultProps = {
    props: {
        size: '150px',
        src: defaultImage,
        alt: 'error'
    }
}

When I turn off props for this component in another file. The component should load defaultprops. Why is this not happening?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Props is an object, when you do { props } you are accessing props.props instead of props.

Just replace

export const ImageComponent = ({ props }) => {

and

ImageComponent.defaultProps = {
    props: {
        size: '150px',
        src: defaultImage,
        alt: 'error'
    }
}

With

export const ImageComponent = (props) => {
    const {size, src, alt} = props;

or

export const ImageComponent = ({size, src, alt}) => {
    ...

And update propTypes to match (this is probably a typo in your exmaple):


ImageComponent.defaultProps = {
    size: '150px',
    src: defaultImage,
    alt: 'error'
}

To simplify this, and prevent these kinds of mistakes in the future, consider using object destructuring to set defaults instead:

export const ImageComponent = (props) => {
    const {
        size = '150px', 
        src = defaultImage, 
        alt = 'error',
    } = props;

or

export const ImageComponent = ({
  size = '150px', 
  src = defaultImage, 
  alt = 'error',
}) => {
    ...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!