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

A JSX element doesn't show it's content

I am new to React. I’m trying to make a navbar for my app. But I encounter a few problems

  • the contents of the <Btn></Btn> is not visible
  • the css doesn’t affects anything

Tools that I use:

  • Firefox with react developer extension
  • Ubuntu 23.04
  • VS Code
  • Tailwindcss

my index.js file

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

import React from 'react';
import ReactDOM from 'react-dom/client';
import './twcss-out.css';

//Import when we need routing in the future
/*import {
  BrowserRouter as Router,
  Routes,
  Route,
  Navigate,
} from "react-router-dom";
*/

const root = ReactDOM.createRoot(document.getElementById('root'));

/* FOR TESTING ONLY
const Test = (props) => {
  let {title, sectionTitle} = props;
  return (
    <div>
      <h1 className='font-sans'>{title}</h1>
      <p className='font-sans'>{sectionTitle}</p>
      <hr/>
    </div>
  )
}
*/
const Btn = ({content, extraClasses}) => {
  return (
    <button type='button' className={'bg-primary text-white p-2 rounded'+extraClasses}>{content}</button>
  );
}


const RootDiv = () => {
  const textFormatting = {
    B: ' font-bold',
    I: ' font-italic',
    U: ' underline'
  }
  const {B, I, U} = textFormatting; 
  return (
    <div>
      <nav className='bg-primary flex flex-1 flex-col'>
        <h1 className='text-xl font-bold text-white py-2 ms-1'>SkyPress</h1>
        <section role='toolbar' className='rounded bg-green-300 grid grid-cols-3 '>
          <section role='group' className='btn-group'>
              <Btn extraClasses={B}>B</Btn>
              <Btn extraClasses={I}>I</Btn>
              <Btn extraClasses={U}>U</Btn> 
          </section>
        </section>
      </nav>      
    </div>
  ); 
}

root.render(
  <RootDiv/>
);

my index.html file

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="stylesheet" href="../src/twcss-out.css">
    <style>
      .btn-group:first-child {
        border-top-left-radius: 0.25rem;
        border-bottom-left-radius: 0.25rem;
      }
      .btn-group:last-child {
          border-top-right-radius: 0.25rem;
          border-bottom-right-radius: 0.25rem;
      }
    </style>
    <title>React App</title>
  </head>
  <body>
    <noscript>Please enable javascript</noscript>
    <div id="root"></div>
    <script src="../src/index.js"></script>
  </body>
</html>

my console outputs and error

console output

also I see something interesting in the devtools panel

interesting devtools panel

Look at the button element. It somehow doesn’t have anything inside it but in the index.js file there’s the letter B, I and U inside of it

I tried reading the react docs, and ofcourse don’t get any info
I also tried adding the letters B, I, and U to the respective buttons through devtools, it worked and the style are applied, but ofcourse I can’t tell the user to click f12 and fix that for me everytime

>Solution :

Rename the content prop to children.

const Btn = ({children, extraClasses}) => {
  return (
    <button type='button' className={'bg-primary text-white p-2 rounded'+extraClasses}>{children}</button>
  );
}

See https://react.dev/learn/passing-props-to-a-component#passing-jsx-as-children


Alternatively you can explicitly pass the value for that content prop :

<Btn extraClasses={B} content='B'/>
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