Coder Social home page Coder Social logo

ankit18singh / react-propify-methods Goto Github PK

View Code? Open in Web Editor NEW

This project forked from staltz/react-propify-methods

1.0 2.0 0.0 10 KB

A React utility to convert instance methods to props with Observables

License: MIT License

TypeScript 30.17% JavaScript 69.83%

react-propify-methods's Introduction

react-propify-methods

Convert instance methods to Observable props.

A utility function (higher-order component, 'HOC') that takes a React component as input, and returns a React component that behaves like the input but accepts Observable props that will call component instance methods.

๐Ÿ˜ž Avoid this: this.textFieldRef.current.clear()

๐Ÿ˜Ž Do this: <TextField clear$={observable} />

What problem this package solves

Let's say you have a React component that has instance methods, for example from React Native we know that TextInput has the clear() method:

<TextInput
  ref={input => {
    this.textInput = input;
  }}
/>
this.textInput.current.clear();

Instance methods are unfortunately necessary in React because props are not enough to trigger the instance calls. So we require Refs, and maintaining refs is not the nicest way of using React.

Usage

npm install --save react-propify-methods

Let's suppose you want to use FlatList's scrollToIndex method using Observable props.

import { FlatList } from 'react-native';
import { propifyMethods } from 'react-propify-methods';

const MyFlatList = propifyMethods(FlatList, 'scrollToIndex', 'scrollToEnd');

// ... then in a render function ...
<MyFlatList
  scrollToIndex$={obs} // this!
  scrollToEnd$={anotherObs} // and this!
  data={this.state.data}
  renderItem={({ item }) => <Item data={item} />}
/>;

The obs argument can be an Observable, from RxJS or simply any object with the subscribe(observer) method. For instance, with Callbags:

import { pipe, interval, map } from 'callbag-basics';
import toObservable from 'callbag-to-obs';

const obs = pipe(
  interval(2000),
  map(i => ({ index: i, viewOffset: 50 })),
  toObservable,
);

// ... in a render function ...
<MyFlatList
  scrollToIndex$={obs}
  data={this.state.data}
  renderItem={({ item }) => <Item data={item} />}
/>;

This will cause the FlatList to scroll to the next item, every 2 seconds. You could have also done it with RxJS:

import Rx from 'rxjs';

const obs = Rx.Observable.interval(2000).map(i => ({
  index: i,
  viewOffset: 50,
}));

API

  • propifyMethods(component, ...methodNames)

Will return a React component that supports a prop named name+$ for every name in methodNames. For example, the setValue method would become a setValue$ prop. Existing props of component are supported by the returned component.

Every new $ prop expects the value to be an Observable. It should be an object with the function subscribe(observer): Subscription or any object that follows the ECMAScript Observable proposal.

If the Observable prop emits a non-array value, then that value is used as the first argument of the method. E.g. setValue$={Observable.of(42)} would call the method setValue(42).

If the Observable prop emits an array, then the array is interpreted as arguments to be applied on the method. E.g. setValue$={Observable.of([42, 'hello'])} would call the method setValue(42, 'hello').

This library only supports component methods that return void. If a non-void value is returned from the method, then it cannot be retrieved through this API.

License

MIT

react-propify-methods's People

Contributors

staltz avatar

Watchers

James Cloos avatar Ankit Singh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.