Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get children components in nextjs?

I need to build a hook to do some generic styles and use it around my project. I tried to do the following:

export const useTextStyles = () => {
  const styles = {
    TextMd,
  };

  return styles;
};

const TextMd: React.FC<{ className: string }> = ({ className }) => {
  return (
    <p
      className={`block transform transition-colors duration-200 py-2 hover:text-gray-800 text-gray-800 .leading-relaxed ${className}`}
    ></p>
  );
};

I use it around like this:

import { useTextStyles } from '../utils/stiles.tsx'
//...
const { TextMd } = useTextStyles();
//into html code:
<TextMd>Hello world</TextMd>

This doesn’t work, probably because I have to pass the children explicitely.. Maybe I am doing it wrong.. In that case what is the best solution to do that in nextjs?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just add the children property

import {ReactNode} from 'react' 

const TextMd: React.FC<{ className: string, children: ReactNode }> = ({ className, children }) => {
  return (
    <p
      className={`block transform transition-colors duration-200 py-2 hover:text-gray-800 text-gray-800 .leading-relaxed ${className}`}
    >{children}</p>
  );
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading