Giter Site home page Giter Site logo

Comments (19)

Grsmto avatar Grsmto commented on April 19, 2024 7

I was thinking more of something along these lines:

const defaultProps = {
  foo: 'bar',
};

type Props = typeof defaultProps & {
  optional?: string
};

export const TestFunction: FC<Props> = props => {
  const { foo } = {
    ...defaultProps,
    ...props,
  };
  return (
    <div>{foo}</div>
  );
};

As that's the pattern that is currently advised in the doc.

from react.

eps1lon avatar eps1lon commented on April 19, 2024 6

as a side note - i dont find myself ever using defaultProps for function components as i can assign when i destructure...

@sw-yx Well looks like you're not the only one: Deprecate defaultProps on function components

from react.

eps1lon avatar eps1lon commented on April 19, 2024 5

It's not clear to me if we should just blanket advise against using React.FC altogether

I wouldn't do that either. I think it's perfectly fine to explain the drawbacks of

explicit annotations on function expressions:

  • -no defaultProps
  • +explicit type annotation (helps with some gotchas concerning return types of function components)
  • -no additional statics
  • -no generic props

function declarations:

  • +defaultProps in ts >= 3.1
  • no implicit children
  • -needs explicit return type to catch wrong return type
  • +additional statics
  • +generic props

Personally I don't like implicit children anyway. Not all of my components handle children. I would hope typescript would recognize some day that <Foo>bar</Foo> is equivalent to <Foo children="bar" /> and report an error on both call sites if the props of Foo don't define a children property.

from react.

ferdaber avatar ferdaber commented on April 19, 2024 5

Default values inside parameters (and when destructured) are better understood by the compiler. It will allow you to not have to hack together the props interface. The compiler knows it will always be defined inside the implementation but optional when consumed.

from react.

swyxio avatar swyxio commented on April 19, 2024 4

thanks - i’ll update accordingly. why is it considered a bad idea to have implicitly typed children? i thought that was a major benefit of using React.FC.

as a side note - i dont find myself ever using defaultProps for function components as i can assign when i destructure...

from react.

ferdaber avatar ferdaber commented on April 19, 2024 2

If I could rewrite react types implicit children would be removed. It causes a lot more problems and was only placed there for convenience.

I always explicitly include children in my props interfaces anyway when I do support them.

from react.

slikts avatar slikts commented on April 19, 2024 2

The linter error should be ignored or turned off, because destructuring and default values already achieves the same as .defaultProps. The linter errors are just rules of thumb that are contextual, and this specific rule doesn't apply in this case.

from react.

swyxio avatar swyxio commented on April 19, 2024 1

i mean.. destructure and assign defaults?

const TestFunction: FunctionComponent<Props> = { foo = "bar" } => <div>{foo}</div>

from react.

ferdaber avatar ferdaber commented on April 19, 2024

Some more context on the issue. I even forgot about it... shame on me. DefinitelyTyped/DefinitelyTyped#30695

from react.

eps1lon avatar eps1lon commented on April 19, 2024

as a side note - i dont find myself ever using defaultProps for function components as i can assign when i destructure...

  1. Makes parsing a component easier: You don't have to look through the function body to know what the default props are. Even more so you don't have to care about the specific component implementation. Just scan for defaultProps and ignore whether it is a class, function, forwardRef etc.
  2. tools like react-docgen only understand defaultProps Actually I'm not sure how good react-docgen is in this regard. Does it only know function ({ foo = 'default' }) or can it find the default value reliably?

from react.

swyxio avatar swyxio commented on April 19, 2024

i've put in something quick to reflect this info. It's not clear to me if we should just blanket advise against using React.FC altogether, that feels like a very strong recommendation that we'd have to justify just because of defaultProps. I'm open to it, but simply havent done it yet. Happy to hear your thoughts.

from react.

swyxio avatar swyxio commented on April 19, 2024

cool cool i'll incorporate more or less what @eps1lon suggested. makes sense

from react.

Grsmto avatar Grsmto commented on April 19, 2024

That's great to know that React actually think about deprecating defaultProps. Could we come up with a pattern that works and does not use defaultProps?

from react.

Grsmto avatar Grsmto commented on April 19, 2024

Right.
You would also have to define your type interface props as optional for all the ones that have a default value. Since you're not using defaultProps anymore.

from react.

slikts avatar slikts commented on April 19, 2024

{ foo = "bar" } => <div>{foo}</div> isn't valid syntax, ({ foo = "bar" }) is, and it would also require = {} at the end or else the caller would need to provide an empty object to use the defaults. So:

const TestFunction: FunctionComponent<Props> = ({ foo = "bar" } = {}) => <div>{foo}</div>

Also, there isn't really much reason to use FC with TS and function components, as .defaultProps, .propTypes and .displayName have better alternatives, and .contextTypes is a legacy feature, so, given the nullable .children typing issue FC has, it probably should just be ({ foo = "bar" }: Props = {}).

from react.

swyxio avatar swyxio commented on April 19, 2024

yep y'all know what i mean

from react.

swyxio avatar swyxio commented on April 19, 2024

going to close this now since it seems we havent had much complaints on this front after Seb's initial recommendations

from react.

StudioSpindle avatar StudioSpindle commented on April 19, 2024

@slikts

"...as .defaultProps, .propTypes and .displayName have better alternatives,..."

I'm having trouble finding the alternative to default props for a functional component.

In the Readme the link to Martin Hochels article mentions you can use type inference for a class component. But that does not work for an FC component. In an earlier article by the same author (from 2018) he mentions a custom getProps function. That covers all the use-cases as explained in the article, but it seems a bit perculiar.

Using prop destructuring gives a linting error (require-default-props). Should this be ignored?

Is that the way to go? An example would help.

from react.

darkowic avatar darkowic commented on April 19, 2024

We found a little bit of casting here to be a good compromise. Obviously, if you don't need defaultProps, the best is to use what Sebastian has suggested (we need this to generate docs).

import React from 'react';
import PropTypes from 'prop-types';

export type MyComponentProps = {
    optionalButDefaulted?: string;
};

const defaultProps = {
    optionalButDefaulted: 'optionalButDefaulted',
};

export const MyComponent = (props: MyComponentProps) => {
    const { optionalButDefaulted } = props as MyComponentProps &
        typeof defaultProps;

    return <div>The string length: {optionalButDefaulted.length}</div>;
};

export const myComponentPropTypes: React.WeakValidationMap<MyComponentProps> = {
    optionalButDefaulted: PropTypes.string,
};

MyComponent.propTypes = myComponentPropTypes;
MyComponent.defaultProps = defaultProps;
MyComponent.displayName = 'MyComponent';

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.