Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean Value Not Rendered in MUI Typography Component #42641

Closed
LokeshBoran opened this issue Jun 14, 2024 · 1 comment
Closed

Boolean Value Not Rendered in MUI Typography Component #42641

LokeshBoran opened this issue Jun 14, 2024 · 1 comment
Assignees
Labels
component: Typography The React component. support: question Community support but can be turned into an improvement

Comments

@LokeshBoran
Copy link

LokeshBoran commented Jun 14, 2024

Steps to reproduce

Link to live example: (required)
CodeSend Box
Stackblitz

Steps to Reproduce:

  1. Create a simple React component using MUI's Typography.
  2. Assign a boolean value to a variable.
  3. Attempt to render this boolean value directly inside the Typography component.

Here is a code sample that demonstrates the issue:

import * as React from 'react';
import Typography from '@mui/material/Typography';

export default function Types() {
  var customValue = true; // Boolean value
  return (
    <Typography variant="h1">
      {customValue} // Attempting to render a boolean value
    </Typography>
  );
}

Workaround:
A workaround is to convert the boolean value to a string before rendering it. This can be done using String(customValue) or customValue.toString() or JSON.stringify(customValue).

Example workaround:

import * as React from 'react';
import Typography from '@mui/material/Typography';

export default function Types() {
  var customValue = true;
  return (
    <Typography variant="h1">
      {String(customValue)} // Converts boolean to string
    </Typography>
  );
}

Environment:

  1. MUI Version: 5.x.x (Please specify the exact version you're using)
  2. React Version: 17.x.x or 18.x.x (Please specify the exact version you're using)
  3. Browser: (Please specify the browser and version)
  4. Operating System: (Please specify the OS and version)

Additional Information:
This issue occurs because Typography (or its underlying rendering mechanism) does not implicitly convert boolean values to strings. Boolean values are not typically meant to be directly rendered in most UI components.

It would be helpful to provide an enhancement or a warning to developers to convert boolean values to strings before rendering.

Suggested Fix:
Enhance the Typography component to handle boolean values gracefully by converting them to their string representation before rendering. This can be implemented by checking the type of the children prop and converting boolean values as needed.

Screenshot
image

Current behavior

The Typography component does not render anything when a boolean value is provided directly.

Expected behavior

The Typography component should display the string representation of the boolean value, i.e., it should render true or false.

Context

When using the MUI (Material-UI) Typography component to render a boolean value directly within the component, the boolean value is not displayed on the UI. Instead of rendering true or false, nothing is shown. This issue can lead to confusion, especially when trying to debug UI elements that should display boolean states.

Your environment

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: MUI Typography, Boolean value MUI Typography

@LokeshBoran LokeshBoran added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jun 14, 2024
@zannager zannager added the component: Typography The React component. label Jun 14, 2024
@sai6855
Copy link
Contributor

sai6855 commented Jun 15, 2024

@LokeshBoran react it's self cannot render boolean values. this is not a limitation with Typography component, In below screenshot you can see, true, false are not rendered when wrapped with in p

image

@sai6855 sai6855 added support: question Community support but can be turned into an improvement and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jun 15, 2024
@sai6855 sai6855 closed this as completed Jun 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: Typography The React component. support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

4 participants