Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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>)
}

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages