babel-plugin-inline-react-svg icon indicating copy to clipboard operation
babel-plugin-inline-react-svg copied to clipboard

`defaultProps` is deprecated for fn components

Open Grohden opened this issue 1 year ago • 9 comments
trafficstars

Message from react: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead

https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-deprecated-react-apis

Grohden avatar May 14 '24 21:05 Grohden

Indeed, we should add an option that skips adding the defaultProps, and uses inline defaulting in the function body instead (we can't use parameter default syntax or ??, since babel won't be applied to our output)

ljharb avatar May 14 '24 21:05 ljharb

Good to read that this issue will be addressed. Any idea when a fix will be released?

baeroe avatar Jun 28 '24 09:06 baeroe

Presumably when someone files a PR adding that option (not a fix, because nothing’s broken, especially since react 19 isn’t even out yet)

ljharb avatar Jun 28 '24 11:06 ljharb

While it is technically true that nothing is broken, those using this plugin to compile their SVG now have their console chock-full of warnings, which makes it harder to see the more useful content in there. Which is not to say that a call for a PR isn't fair :)

hon2a avatar Jul 17 '24 15:07 hon2a

My job has a script that will fail any tests if they throw unaddressed warnings, so this has actually caused pretty big problems for me.

ryanfiller avatar Jul 22 '24 17:07 ryanfiller

when I opened the issue I took a little look into how to solve.. but like, I don't really know what level of compatibility is wanted for this one, but if we can use Object.assign, in theory we could do something along these lines (?):

    const defaultProps = SVG_DEFAULT_PROPS_CODE 
          ? `const props = Object.assign({}, SVG_DEFAULT_PROPS_CODE, overrides)` 
          : `const props = overrides`;
    const namedTemplate = `
      var SVG_NAME = function SVG_NAME(overrides) { ${defaultProps}; return SVG_CODE; };
      ${IS_EXPORT ? 'export { SVG_NAME };' : ''}
    `;
    const anonymousTemplate = `
      var Component = function (overrides) { ${defaultProps}; return SVG_CODE; };
      Component.displayName = 'EXPORT_FILENAME';
      export default Component;
    `;

for: https://github.com/airbnb/babel-plugin-inline-react-svg/blob/master/src/index.js#L29-L39

Grohden avatar Jul 22 '24 21:07 Grohden

I used patch-package and used @Grohden's approach and that resolves everything for me. He does raise a good point on what level of compatibility in the future but this can work.

I'd wonder what other's mileage looks like with this in place

ghost1face avatar Dec 04 '24 16:12 ghost1face

@ghost1face I ended up just putting -defaultProps in the Chrome DevTools console filter bar and forgetting about the whole thing.

hon2a avatar Dec 04 '24 16:12 hon2a

@Grohden we can't use Object.assign but we can use the same semantics via the object.assign package. @Grohden, a PR would be appreciated.

ljharb avatar Dec 04 '24 17:12 ljharb