Skip to content

cenarturkmen/custom-hooks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CUSTOM HOOKS

This is easier than it looks. Basicly you need to create a file called 'use-*.js' in somewhere in your project. And then you need to move your statetful logic to that file.

Example

You have a code like this ->

import { useState } from 'react'

export const component () => {
    const [inputs, setInputs] = useState({})

    const handleChange = (e) => {
        setInputs({
            ...inputs,
            [e.target.name]: e.target.value
        })
    }
    return (
        <div>
            <input type="text" name="name" onChange={handleChange} />
            <input type="text" name="age" onChange={handleChange} />
        </div>
    )
    )
}

You can use the hook like this ->

import { useState } from "react";

export const useInputs = (params) => {
  const [inputs, setInputs] = useState({ params });

  const handleChange = (e) => {
    setInputs({
      ...inputs,
      [e.target.name]: e.target.value,
    });
  };

  return [inputs, handleChange];
};

And then on your component you can use it like this ->

import { useInputs } from "./use-inputs";
export const component = () => {
    const [inputs, handleChange] = useInputs({name: "", age: ""});
    return (
        <div>
        <input type="text" name="name" onChange={handleChange} />
        <input type="text" name="age" onChange={handleChange} />
        </div>
    );
    };
}

This is a so basic example but this is the all you need to do to use it.

About

custom hook examples

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy