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-functional-lifecycle

v3.0.0

Published

Access lifecycle methods without a class or a component state

Downloads

663

Readme

React Functional Lifecycle

Are you tired of using classes in React just to use the lifecycle methods? This library is designed to allow lifecycle methods to be exposed as composable functions.

Previously:

class MyComponent {

    componentDidMount () {
        console.log('I mounted with props:', this.props);
    }

    componentWillUnmount () {
        console.log('See you later!', this.props);
    }

    getSnapshotBeforeUpdate () {
        return 'I am a snapshot!';
    }

    componentDidUpdate (prevProps, prevState, snapshot) {
        console.log('Check out my snapshot: ', snapshot);
    }

    render () {
        return (
            <SomeComponent {...this.props} />
        );
    }

}

With React-Functional-LifeCycle:


const componentMount = props => {
    console.log('I mounted with props:', props);
};

const getSnapshot = () => (
    'I am a snapshot!'
);

const componentUpdate = (props, prevProps, snapshot) => {
    console.log('Check out my snapshot: ', snapshot);
};

const componentUnmount = props => {
    console.log('See you later!', props);
};

const MyComponent = compose(
    componentDidMount(componentMount),
    getSnapshotBeforeUpdate(getSnapshot, componentUpdate)
    componentWillUnmount(componentUnmount)
)(SomeComponent);

This is a way to tap into the React lifecycle methods without writing classes. This is useful if you are using Recompose or Redux to manage your state, but you still need to access lifecycle methods.

This library works with React web or native and allows access to all of the lifecycle methods you know and love.

Installation

React Functional Lifecycle has a peerDependency of React 14 or greater. Otherwise, installing it is as easy as:

npm install react-functional-lifecycle --save

API

Table of Contents

componentWillMount

This is triggered when a component will mount. Note that this method is being deprecated and will not work in version 17 of React. Use UNSAFE_componentWillMount if you must use it now.

componentWillMount(props: Object, component: Object)

import { componentWillMount } from 'react-functional-lifecycle';

export default componentWillMount(props => {
    console.log('I will mount with props:', props);
})(YourReactComponent);

UNSAFE_componentWillMount

This is triggered when a component will mount.

UNSAFE_componentWillMount(props: Object, component: Object)

import { UNSAFE_componentWillMount } from 'react-functional-lifecycle';

export default UNSAFE_componentWillMount(props => {
    console.log('I will mount with props:', props);
})(YourReactComponent);

componentWillReceiveProps

This is triggered when the component is about to get new props. Note that this method is being deprecated and will not work in version 17 of React. Use UNSAFE_componentWillReceiveProps if you must use it now.

componentWillReceiveProps(props: Object, component: Object)

import { componentWillReceiveProps } from 'react-functional-lifecycle';

export default componentWillReceiveProps(props => {
    console.log('I am going to receive props:', props);
})(YourReactComponent);

UNSAFE_componentWillReceiveProps

This is triggered when the component is about to get new props.

UNSAFE_componentWillReceiveProps(props: Object, component: Object)

import { UNSAFE_componentWillReceiveProps } from 'react-functional-lifecycle';

export default UNSAFE_componentWillReceiveProps(props => {
    console.log('I am going to receive props:', props);
})(YourReactComponent);

componentDidMount

This is triggered after a component mounts.

componenDidMount(props: Object, component: Object)

import { componenDidMount } from 'react-functional-lifecycle';

export default componenDidMount(props => {
    console.log('I did mount with props:', props);
})(YourReactComponent);

shouldComponentUpdate

Return true to update your component and false to prevent the component from re-rendering.

shouldComponentUpdate(props: Object, nextProps: Object, component: Object)

import { shouldComponentUpdate } from 'react-functional-lifecycle';

export default shouldComponentUpdate(
    (props, nextProps) => props !== nextProps
)(YourReactComponent);

componentWillUpdate

This is triggered when a component will update. Note that this method is being deprecated and will not work in version 17 of React. Use UNSAFE_componentWillUpdate if you must use it now.

componentWillUpdate(props: Object, nextProps: Object, component: Object)

import { componentWillUpdate } from 'react-functional-lifecycle';

export default componentWillUpdate((props, nextProps) => {
    console.log('I will update with props:', nextProps);
    console.log('Here are my current props', props);
})(YourReactComponent);

UNSAFE_componentWillUpdate

This is triggered when a component will update.

UNSAFE_componentWillUpdate(props: Object, nextProps: Object, component: Object)

import { UNSAFE_componentWillUpdate } from 'react-functional-lifecycle';

export default UNSAFE_componentWillUpdate((props, nextProps) => {
    console.log('I will update with props:', nextProps);
    console.log('Here are my current props', props);
})(YourReactComponent);

componentDidUpdate

This is triggered when a component finished an update.

componentDidUpdate(props: Object, prevProps: Object, component: Object)

import { componentDidUpdate } from 'react-functional-lifecycle';

export default componentDidUpdate((props, prevProps) => {
    console.log('I did update with props:', props);
    console.log('Here are my previous props', prevProps);
})(YourReactComponent);

componentWillUnmount

This is triggered when a component is about to unmount.

componentWillUnmount(props: Object, component: Object)

import { componentWillUnmount } from 'react-functional-lifecycle';

export default componentWillUnmount((props) => {
    console.log('I will unmount with props:', props);
})(YourReactComponent);

getDerivedStateFromProps

This is invoked after a component is instantiated as well as when it receives new props. It should return an object to update state, or null to indicate that the new props do not require any state updates.

getDerivedStateFromProps(props: Object)

import { getDerivedStateFromProps } from 'react-functional-lifecycle';

export default getDerivedStateFromProps((props) => {
    return !!props.hasUpdate;
})(YourReactComponent);

getSnapshotBeforeUpdate

This method takes two functions: the getSnapshotBeforeUpdate method and the componentDidUpdate method which has access to the snapshot. getSnapshotBeforeUpdate() is invoked right before the most recently rendered output is committed to e.g. the DOM. It enables your component to capture current values (e.g. scroll position) before they are potentially changed. Any value returned by this lifecycle will be passed as a parameter to componentDidUpdate()

getSnapshotBeforeUpdate([props: Object, prevProps: Object, component: Object], [props: Object, prevProps: Object, snapshot: any, component: Object])

import { getSnapshotBeforeUpdate } from 'react-functional-lifecycle';

export default getSnapshotBeforeUpdate((props, prevProps) => {
    return 'MY SNAPSHOT'
}, (props, prevProps, snapshot) => {
    console.log(snapshot); // MY SNAPSHOT
})(YourReactComponent);

componentDidCatch

This is triggered when a component has an error.

componentDidCatch(props: Object, error: any, info: any, component: Object)

import { componentDidCatch } from 'react-functional-lifecycle';

export default componentDidCatch((_props, error) => {
    console.log('I had an error:', error);
})(YourReactComponent);

Composing LifeCycle Methods

The composed wrapper usage allows you to string multiple methods together. If you are using a utility library such as Recompose, Lodash or Ramda, you can use compose like this:

import { compose } from 'recompose';
import { shouldComponentUpdate } from 'react-functional-lifecycle';

export default compose(
    shouldComponentUpdate((props, nextProps) => props !== nextProps),
    componentWillUnmount(() => console.log('Bye!'))
)(YourReactComponent);

Contributing

We encourage you to contribute to React Functional Lifecycle by submitting bug reports and pull requests through Github.

License

React Functional Lifecycle is released under The MIT License (MIT)

Copyright (c) [2018] [Aloompa LLC]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.