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

babel-plugin-stateful-functional-react-components

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-stateful-functional-react-components

Stateful functional React components without runtime overhead

  • 0.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-stateful-functional-react-components

Stateful functional React components without runtime overhead (inspired by ClojureScript)

Compiles stateful functional React components syntax into ES2015 classes

WARNING: This plugin is experimental. If you are interested in taking this further, please open an issue or submit a PR with improvements.

npm

Why?

Because functional components are concise and it's annoying to write ES2015 classes when all you need is local state.

Advantages

  • No runtime overhead
  • No dependencies that adds additional KB's to your bundle

Example

Input

//                props      context   state    init state
const Counter = ({ text }, { theme }, { val } = { val: 0 }, setState) => (
  <div className={theme}>
    <h1>{text}</h1>
    <div>
      <button onClick={() => setState({ val: val - 1 })}>-</button>
      <span>{val}</span>
      <button onClick={() => setState({ val: val + 1 })}>+</button>
    </div>
  </div>
);

Output

class Counter extends React.Component {
  constructor() {
    super();
    this.state = { val: 0 };
  }
  render() {

    const { text } = this.props;
    const { theme } = this.context;
    const { val } = this.state;

    return (
      <div className={theme}>
        <h1>{text}</h1>
        <div>
          <button onClick={() => this.setState({ val: val - 1 })}>-</button>
          <span>{val}</span>
          <button onClick={() => this.setState({ val: val + 1 })}>+</button>
        </div>
      </div>
    );
  }
}

API

(props [,context], state = initialState, setState)

  • props is component’s props i.e. this.props
  • context is optional parameter which corresponds to React’s context
  • state is component’s state, initialState is required
  • setState maps to this.setState

Requirements

  • state parameter must be assigned default value (initial state)
  • The last parameter must be named setState

Installation

npm i babel-plugin-stateful-functional-react-components

Usage

.babelrc

{
  "plugins": ["stateful-functional-react-components"]
}

Via CLI

babel --plugins stateful-functional-react-components script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["stateful-functional-react-components"]
});

License

MIT

Keywords

FAQs

Package last updated on 02 Jun 2017

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