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

react-component-ref

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-component-ref

Simple helper class to help you stay DRY (Don't repeat yourself)

  • 1.0.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
decreased by-75%
Maintainers
1
Weekly downloads
 
Created
Source

react-component-ref

Simple helper class to help you stay DRY (Don't repeat yourself)

npm version license Build Status Code Climate Test Coverage Issue Count TypeScript Typings

ComponentRef is a small helper class which should help you stay as much DRY as possible and encourage you to add refs to your react Components when it's necessary (when you need it or a third party library needs access to native element). (see Refs to Components if you haven't already)

Install

Install with npm:

$ npm install react-component-ref

NPM

Usage

JavaScript

// before

class Before extends React.Component {
  constructor(props) {
    super(props);
    this.inputName = null;
    this.inputPass = null;
    this.refName = (input) => {
      this.inputName = input;
      input.focus();
    }
    this.refInput = (input) => this.inputPass = input;
    this.onChange = (event) => this.inputName.select();
  }
  
  render() {
    return (
      <form name="login">
        <input name="name" ref={this.refName} onChange={this.onChange}/>
        <input name="pass" ref=(this.refPass} />
      <form>
    );
  }
}

// after

import ComponetRef from 'react-component-ref';

class After extends React.Component {
  constructor(props) {
    super(props);
    this.inputName = new ComponentRef((input) => input.focus());
    this.inputPass = new ComponentRef();
    this.onChange = (event) => this.inputName.getComponent().select();
  }
  
  render() {
    return (
      <form name="login">
        <input name="name" ref={this.inputName.ref} onChange={this.onChange}/>
        <input name="pass" ref=(this.inputPass.ref} />
      <form>
    );
  }
}

TypeScript

// before

class Before extends React.Component<any, any> {
  private inputName: HTMLInputElement;
  private inputPass: HTMLInputElement;
  private refName: (input: HTMLInputElement) => void;
  private refPass: (input: HTMLInputElement) => void;
  private onChange: React.FromEventHandler;
  
  public constructor(props: any) {
    super(props);
    this.inputName = null;
    this.inputPass = null;
    this.refName = (input: HTMLInputElement): void => {
      this.inputName = input;
      input.focus();
    }
    this.refInput = (input: HTMLInputElement): void => this.inputPass = input;
    this.onChange = (event: React.FormEvent): void => this.inputName.select();
  }
  
  render() {
    return (
      <form name="login">
        <input name="name" ref={this.refName} onChange={this.onChange}/>
        <input name="pass" ref=(this.refPass} />
      <form>
    );
  }
}

// after

import ComponetRef from 'react-component-ref';
import bind from 'bind-decorator'; // Optional 

class After extends React.Component<any, any> {
  private inputName: ComponetRef<HTMLInputElement>;
  private inputPass: ComponetRef<HTMLInputElement>;
  
  @bind
  private onChange(event: React.FormEvent): void {
    this.inputName.getComponent().select();
  }
  
  public constructor(props: any) {
    super(props);
    this.inputName = new ComponentRef<HTMLInputElement>((input: HTMLInputElement) => input.focus());
    this.inputPass = new ComponentRef<HTMLInputElement>();
  }
  
  public render(): JSX.Element {
    return (
      <form name="login">
        <input name="name" ref={this.refName.ref} onChange={this.onChange}/>
        <input name="pass" ref=(this.refPass.ref} />
      <form>
    );
  }
}

Testing

  1. npm install

  2. npm test

Contributing

  1. npm install

  2. Make changes

  3. If necessary add some tests to __tests__

  4. npm test

  5. Make a Pull Request

Keywords

FAQs

Package last updated on 19 Oct 2016

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