31

Could anyone show an example of defining defaultProps on a React component class in TypeScript?

interface IProps {}
interface IState {}

class SomeComponent extends Component<IProps, IState> {
    // ... defaultProps ?
    // public defaultProps: IProps = {}; // This statement produces an error

    constructor(props: IProps) {
        super(props);
    }

    // ...
}

3 Answers 3

56

You can define default props this way:

export class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {count: props.initialCount};
    this.tick = this.tick.bind(this);
  }
  tick() {
    this.setState({count: this.state.count + 1});
  }
  render() {
    return (
      <div onClick={this.tick}>
        Clicks: {this.state.count}
      </div>
    );
  }
}
Counter.propTypes = { initialCount: React.PropTypes.number };
Counter.defaultProps = { initialCount: 0 };

This is equivalent in TypeScript to defining defaultProps as a static field inside the class body:

class SomeComponent extends Component<IProps, IStates> {
    public static defaultProps: IProps = { /* ... */ }; 
    // ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

For this to work with TypeScript, you should cast the object to IProps by appending ' as IProps' just before the semicolon.
The provided link no longer discusses defaultProps. Instead see facebook.github.io/react/docs/react-component.html#defaultprops
Thanks @WoodenKitty, been trying to find the docs on that for a while. Google was no help
FWIW - Airbnb's stlyeguide follows this pattern as well. github.com/airbnb/javascript/tree/master/react#ordering
this doesn't seem to work... If I commit one of the items from the jsx, it throws an exception. It works if I make it optional ` title? : String;`... Is that what is suppose to happen?
|
0

Let say if you have a Movie stateless component then you can define proptype like this below:

const Movie = props => {
    return (
       <div>
         <h3>{props.movie.title}</h3>
       </div>
    );
 };

Movie.propTypes = {
  movie: PropTypes.shape({
     title: PropTypes.string.isRequired
  })
};

Movie. defaultProps = {
  movie: PropTypes.shape({})
};

And for class component you can do something like this or you can use the same pattern like above:

export default class Movie extends React.Component {
  static propTypes = {
  movie: PropTypes.shape({
  title: PropTypes.string.isRequired
  }),
  desc: PropTypes.string
};

 static defaultProps = {
  desc: 'No movie is available'
 };

render() {
  return (
     <div>
        <h3>{this.props.movie.title}</h3>
        <h3>{this.props.movie.desc}</h3>
     </div>
    );
 }
}

2 Comments

Did you mean Movie.defaultProps = { (with an "s")?
and where is typescript here?
0

I used if-statements to check if the value of the prop was undefined, if so, I set a default value, otherwise I used the value that was passed through.

interface Props {
  style: any;
  bounces?: boolean | undefined;
  extraHeight?: number | undefined;
}

const DynamicView: React.FC<Props> = (props) => {
  return (
    <KeyboardAwareScrollView
      style={props.style}
      bounces={
        (props.bounces = props.bounces === undefined ? false : props.bounces)
      }
      extraHeight={
        (props.extraHeight =
          props.extraHeight === undefined ? 15 : props.extraHeight)
      }>
      {props.children}
    </KeyboardAwareScrollView>
  );
};

export default DynamicView;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.