how to make custom hooks in react? #298
Answered
by
joelgenaro
albayanidev
asked this question in
Q&A
-
I'm making a chatting react project and need to make custom hooks, help me! |
Beta Was this translation helpful? Give feedback.
Answered by
joelgenaro
Apr 8, 2024
Replies: 1 comment 1 reply
-
Hi here is my solution for that: function useCustomHook() { const increment = () => { return { count, increment }; import React from 'react'; function MyComponent() { return ( Count: {count} Increment ); } export default MyComponent; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
albayanidev
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;