1
votes

I'm working with react storybook and wondering how can I set default props for a component?

My component within my app is a rail and fetches data - an image. This is set using {this.props.children}.

Within my rail component, I want to set default props so that the items still render if the data fetch fails.

Here is my component - scroll down the bottom for setting defaultProps:

import React, { Component } from 'react';
import './index.css'; // styles from

// import Slider from 'react-slick';
import classNames from 'classnames';

import PrimaryButton from '../PrimaryButton';

export default class HeroRail extends Component{
  render() {
    const {title, icon, opacity, railActive} = this.props;
    const railStyle = {
      opacity:opacity
    };
    const iconStyle = {
      background: `center / contain url(${icon}) no-repeat`
    };
    const cName = classNames('HeroRail', {
      railActive:railActive
    });   

    console.log("props", this.props);

    return (
      <div className={cName} style={railStyle}>
        <div className={'rail-details'}>
          <div className={'top-details'}>
            <div className={'title-lockup'}>
              <div className={'icon'} style={iconStyle}></div>
              <h3 className={'rail-title'}>{title}</h3>
            </div>
            <PrimaryButton label={'View All'} light={true}/>
          </div>

          <div className={'rail-navigation'}>
            <PrimaryButton icon='' light={true} />
            <PrimaryButton icon='' light={true} />
          </div>
        </div>

        <div className={'rail-items'}>
          {this.props.children}
        </div>
      </div>
    );
  }
};

HeroRail.defaultProps = {

};

Here is what the children prop looks like when a fetch is successful.

enter image description here

3

3 Answers

1
votes

You can set the default props using the class name as shown below.

class Greeting extends React.Component {
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}

// Specifies the default values for props:
Greeting.defaultProps = {
  name: 'Stranger'
};

You may refer this link from react's documentation for more info.

0
votes

one way to slove the issue is to use static propTypes

static propTypes = {
    children: PropTypes.node.isRequired,
}

another way to slove the issue wish i recomend , is to not render the component unless you have fetched the value

if(!this.props.children){
    return(<div>Loading ....... </div>
}
 return (
      <div className={cName} style={railStyle}>
        <div className={'rail-details'}>
          <div className={'top-details'}>
            <div className={'title-lockup'}>
              <div className={'icon'} style={iconStyle}></div>
              <h3 className={'rail-title'}>{title}</h3>
            </div>
            <PrimaryButton label={'View All'} light={true}/>
          </div>

          <div className={'rail-navigation'}>
            <PrimaryButton icon='' light={true} />
            <PrimaryButton icon='' light={true} />
          </div>
        </div>

        <div className={'rail-items'}>
          {this.props.children}
        </div>
      </div>
    );
0
votes

One way of solving this is to import the 'prop-types' component see the following example:-

import PropTypes from 'prop-types';

const propertyType = ({[name_of_property]}) => {
  <div><h1>{name_of_property}</h1></div>
};

propertyType.propTypes = {
  [name_of_property]: PropTypes.string,
};
propertyType.defaultProps ={
  [name_of_property]: [default_value],
};

read this when you can : https://www.npmjs.com/package/prop-types