Skip to content

how to make custom hooks in react? #298

Answered by joelgenaro
yuriyleve asked this question in Q&A
Discussion options

You must be logged in to vote

Hi here is my solution for that:

function useCustomHook() {
const [count, setCount] = React.useState(0);

const increment = () => {
setCount((prevCount) => prevCount + 1);
};

return { count, increment };
}

import React from 'react';
import useCustomHook from './useCustomHook';

function MyComponent() {
const { count, increment } = useCustomHook();

return (


Count: {count}


Increment

);
}

export default MyComponent;

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@yuriyleve
Comment options

Answer selected by yuriyleve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants