score:125

Accepted answer

here's a similar question with an answer: react with typescript - define defaultprops in stateless function

import react, { component } from 'react';
import { text } from 'react-native';

interface testprops {
    title?: string,
    name?: string
}

const defaultprops: testprops = {
    title: 'mr',
    name: 'mcgee'
}

const test: react.sfc<testprops> = (props) => (
    <text>
        {props.title} {props.name}
    </text>
);

test.defaultprops = defaultprops;

export default test;

score:-5

to me, this doesn't look like a typescript issue.

disclaimer: i have only tried this with typescript.

however, the problem is that props always exists (even as an empty object when nothing is passed in). there are 2 workaround for this, though.

the first, unfortunately, kills the super clean curly-brace-less syntax you have, but let's you keep defaultprops around.

interface testprops {
    title?: string;
    name?: string;
}

const defaultprops: testprops = {
    title: 'mr',
    name: 'mcgee'
}

const test = (passedin: testprops) => {
    const props = object.assign({}, defaultprops, passedin);
    return (
        <p>
            {props.title} {props.name}
        </p>
    );
}

another alternative that might get a little hairy if you have a ton of props, but that lets you keep your original syntax is something like this:

const test = (props: testprops) => (
    <text>
        {props.title || 'mr'} {props.name || 'mcgee'}
    </text>
);

hope this helps!

score:0

i might be wrong, but passing the a default prop value on the function as the second voted reply says could lead to subtle bugs or over executed useeffects (i don't have enough rep to reply there, so here's a reproducible codesanbox)

even if it's a really contrived example, and probably in most of the cases just bad component design, i have seen this more than once, even breaking full pages.

score:1

adding my solution to the pot, i think it adds an additional level of readability and elegance onto the existing solutions.

let's say you have a component mycomponent with a mix of required and optional props. we can separate these required and optional props into two interfaces, combining them for the full prop interface of the component, but only using the optional one to set the default props:

import * as react from "react";

// required props
interface imycomponentrequiredprops {
  title: string;
}

// optional props
interface imycomponentoptionalprops {
  color: string;
  fontsize: number;
}

// combine required and optional props to build the full prop interface
interface imycomponentprops
  extends imycomponentrequiredprops,
    imycomponentoptionalprops {}

// use the optional prop interface to define the default props
const defaultprops: imycomponentoptionalprops = {
  color: "red",
  fontsize: 40,
};

// use the full props within the actual component
const mycomponent = (props: imycomponentprops) => {
  const { title, color, fontsize } = props;
  return <h1 style={{ color, fontsize }}>{title}</h1>;
};

// be sure to set the default props
mycomponent.defaultprops = defaultprops;

export default mycomponent;

score:16

here's how i like to do it:

type testprops = { foo: foo } & defaultprops
type defaultprops = partial<typeof defaultprops>
const defaultprops = {
  title: 'mr',
  name: 'mcgee'
}

const test = (props: props) => {
  props = {...defaultprops, ...props}
  return (
    <text>
      {props.title} {props.name}
    </text>
  )
}

export default test

score:84

i've found the easiest method is to use optional arguments. note that defaultprops will eventually be deprecated on functional components.

example:

interface testprops {
    title?: string;
    name?: string;
}

const test = ({title = 'mr', name = 'mcgee'}: testprops) => {
    return (
        <p>
            {title} {name}
        </p>
    );
}

Related Query

More Query from same tag