react-use-when-mounted
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

React useWhenMounted

Hook provide callback function that will be invoked only if component is mounted

Getting started

yarn add react-use-when-mounted --save

Usage

import React, { useState, useEffect } from 'react'
import { useWhenMounted } from 'react-use-when-mounted'

export const TestExample: React.FC<{}> = () => {
    const whenMounted = useWhenMounted()
    const [data, setData] = useState()
    
    useEffect(() => {
        fetch('http://example.com/movies.json')
            .then(response => response.json())
            .then(data => {
                whenMounted(() => {
                    setData(data)
                })
            })
        },
        [whenMounted]
    )
    
    return (<div>
        {data}
    </div>)
}

Readme

Keywords

Package Sidebar

Install

npm i react-use-when-mounted

Weekly Downloads

5

Version

1.0.1

License

MIT

Unpacked Size

3.8 kB

Total Files

7

Last publish

Collaborators

  • fookoo