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

react-controlled

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-controlled

Useful higher-order-component for react controlled components

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

react-controlled

higher-order-component for react controlled components

Installation

npm install --save react-controlled

Example

simple case

import React from 'react';
import controlled from 'react-controlled';

class MyComponent extends React.Component {
    onClickSave = () => {
        const nickname = this.props.fields('nickname').value;
        const age = this.props.fields('age').value;
    }
    render() {
        return <div>
            <input type="text" {...this.props.fields('nickname')}/>
            <input type="text" {...this.props.fields('age')}/>
            <button onClick={this.onClickSave}>save</button>
        </div>;
    }
}

export default controlled(MyComponent)

with defaultValue

It is recommanded to set default value at constructor.

import React from 'react';
import controlled from 'react-controlled';

class MyComponent extends React.Component {
    constructor(props) {
        super(props);
        this.props.fields({
            fieldName: 'nickname',
            defaultValue: 'no name',
        });
    }
    render() {
        return <div>
            <input type="text" {...this.props.fields('nickname')}/>
        </div>;
    }
}

export default controlled(MyComponent)

customize getValue function

default getValue function: args => args[0].target.value

import React from 'react';
import controlled from 'react-controlled';

class MyComponent extends React.Component {
    constructor(props) {
        super(props);
        this.props.fields({
            fieldName: 'nickname',
            getValue: args => args[0],
        });
    }
    render() {
        return <div>
            <SomeComponent {...this.props.fields('nickname')}/>
        </div>;
    }
}

export default controlled(MyComponent)

getFieldValues, setFieldValues

import React from 'react';
import controlled from 'react-controlled';

class MyComponent extends React.Component {
    onClick = () => {
        const values = this.props.getFieldValues();
        values.nickname = 'my name is ' + values.nickname;
        values.age = 'my age is ' + values.age;
        this.props.setFieldValues(values);
    }
    render() {
        return <div>
            <input type="text" {...this.props.fields('nickname')}/>
            <input type="text" {...this.props.fields('age')}/>
            <button onClick={this.onClick}>example</button>
        </div>;
    }
}

export default controlled(MyComponent)

Keywords

FAQs

Package last updated on 13 Mar 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