Skip to content

johannbuscail/use_referred_state

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to use/import

First of all, download this package with yarn or npm:

//yarn
yarn add use_referred_state
//or npm
npm i use_referred_state

To use this package just import it to your project:

import useReferredState from "use_referred_state";
//or
const useReferredState = require("use_referred_state");

And then call the hook in your functionnal component: In javascript:

const [notes, notesRef, setNotes] = useReferredState();
const [notes, notesRef, setNotes] = useReferredState<string[]>();

This will set a variable notes which will be a state variable and a notesRef variable which wil be a ref variable.

You will need to access the notesRef value by access current property:

notesRef.current === notes;

This setNotes method will update both notesRef and notes variable.

Usage example

import React, { useEffect } from "react"
import axios from "axios"
import useReferredState from "use_referred_state"

const NoteList: React.FC = () => {
    const [notes, notesRef, setNotes] = useReferredState<string[]>([ 'hi', 'bye' ]);
    useEffect(() => {
        axios.get<string[]>('./notes').then((res) => setNotes([ ...notesRef.current, ...res.data]));
    }, []);

    return (
        <>
            <h1>Notes: </h1>
            <ol>
                {notes.map((note) => (
                    <li>{note}</li>
                ))}
            </ol>
        </>
    )
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published