npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-rx-props

v1.0.4

Published

Convert react component props to rx multicasted observables

Downloads

11

Readme

React Rx Props

Build Status codecov

HoC to wrap your component props into observables to specify straightforward rules when component should update.

Installation

npm i --save react-rx-props

You also need to have react@>=15.0.0 and rxjs@>=5.0.0 in your dependencies.

Documentation

reactRxProps:

import { reactRxProps } from 'react-rx-props';

reactRxProps(options)(YourComponent)
Options

If options object not provided - default values will be used.

ignoreProps - array of props that should not be wrapped into observable. By default its ['className', 'style']. Functions and Observables never wrapped into observable.

existName - name of exist param. By default it exist. If you set it to false - exist param will be not passed. Exist param is useful observable that can be used as observable.takeUntil(this.props.exist). It will be emit value once HoC component will unmount. Affected by addDollar.

propTypes - instead specifying prop types in your component pass it here. By default undefined.

defaultProps - instead specifying default props in your component pass it here. By default undefined.

addDollar - if true it will add $ to property name when it wrapped into observable. By default true.

reactRxPropsConnect:

import { reactRxPropsConnect } from 'react-rx-props';

reactRxPropsConnect(options)(YourComponent)
Options

If options object not provided - default values will be used.

connect - this function will be executed before component will be mounted. It have 2 args: props that represent all passed props to this component and render callback that should be executed when you want to render wrapped component. render have single props param that will be used to update current props passed to wrapped component. In addition to this props wrapped component will receive all non Observable props.

propTypes - instead specifying prop types in your component pass it here. By default undefined.

defaultProps - instead specifying default props in your component pass it here. By default undefined.

Motivation

Often after getting some input data we create some internal model of component that allow faster render invocation, for example lets write component that display fibonacci number. It will have next parameters:

className     //Fibonacci calculation is not required after this property change, but we should render.
value         //We should calculate fibonacci and render component after this property change
useServerCall //Rendering is not required after this property change, but it will be used in next calculation

Lets write it in some kind of classic way: FibonacciBasic.js

Pretty complex for such simple task? We need to handle this logic in 4 lifecycle methods. Have a lot of code duplication, shouldComponentUpdate executes not only when props changed but also when we update state. Its pretty easy to write wrong code here, that will run additional renders for example.

Lets update it using React Rx Props library: FibonacciReactRxProps.js

Now all logic placed in componentWillMount. Lets make breakdown to explain what's going on:

componentWillMount() {
  //We are simply save useServerCall as class property (better to put such properties under some object), 
  //no setState call, no render.
  this.props.useServerCall$.subscribe(useServerCall => {
    this.useServerCall = useServerCall;
  });
  
  //On every value change including initial value...
  this.props.value$.switchMap(value => {
    //...we save value as class property
    this.value = value;
    //...we are set loading flag to true in state...
    this.setState({
      loading: true,
    }); 
    //...and execute calculateFibonacci function that will return observable for result...
    return calculateFibonacci(value, this.useServerCall)
      //...We don't want to process result if component already unmounted...
      .takeUntil(this.props.exist$);
  })
  //...update state with calculated fibonacci
  .subscribe(fibonacci => {
    this.setState({
      loading: false,
      fibonacci: fibonacci,
    });
  });
}

I bet you would like to write stateless component instead, lets do it using reactRxPropsConnect: FibonacciStateless.js

When we are working with Redux we can face more complex situations especially when we need to work with multiple async data sources. Dealing with it using Observables will be easier and cleaner in terms of code.