Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-react-defaultprops

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-react-defaultprops

A plugin to extract es6 default parameters

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-95.45%
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-react-defaultprops

A babel plugin that extracts es6 default parameters and append it to the component property named __defaultProps.

Usage

npm install --save-dev babel-plugin-react-defaultprops
yarn add -D babel-plugin-react-defaultprops

Add the plugin to the Babel configuration:

babel.config.js

module.exports = {
plugins: ['module:babel-plugin-react-defaultprops'],
};

Example

Before:

export function FunctionComponent({ bar, foo = 'func' }) {
  return <div>{bar + foo}</div>;
}

After:

function FunctionComponent(_ref) {
  var bar = _ref.bar,
    _ref$foo = _ref.foo,
    foo = _ref$foo === void 0 ? 'func' : _ref$foo;
  return _react['default'].createElement('div', null, bar + foo);
}

FunctionComponent.__defaultProps = {
  foo: 'func',
};

Or with defaults in function body:

Before:

export const VariableComponent = (props) => {
  const { bar, foo = 'variable' } = props;
  return <div>{bar + foo}</div>;
};

After:

var VariableComponent = function VariableComponent(props) {
  var bar = props.bar,
    _props$foo2 = props.foo,
    foo = _props$foo2 === void 0 ? 'variable' : _props$foo2;
  return _react['default'].createElement('div', null, bar + foo);
};

VariableComponent.__defaultProps = {
  foo: 'variable',
};

For more example look into the test folder.

Node that the default props with the locale variable as a value in the function body will not be included.

Utils

  • getDefaultProps

To make object form default props, works only inside the function body.

import React from 'react';
import { getDefaultProps } from 'babel-plugin-react-defaultprops/utils';

export function FunctionComponent({ bar, foo = 'func' }) {
  const defaultProps = getDefaultProps();
  return <div>{bar + foo}</div>;
}

Transform to:

function FunctionComponent(_ref) {
  var bar = _ref.bar,
    _ref$foo = _ref.foo,
    foo = _ref$foo === void 0 ? 'func' : _ref$foo;

  var defaultProps = {
    foo: 'func',
  };

  return _react['default'].createElement('div', null, bar + foo);
}

This function does not work in nested component inside function, works only with top level components.

Typescript

If using typescript ad following to theglobal.d.ts file:

declare module 'react' {
  export interface FunctionComponent<P = unknown> {
    __defaultProps?: Partial<P>;
  }
}
export {};

Keywords

FAQs

Package last updated on 13 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc