score:408
Within the render
method comments are allowed, but in order to use them within JSX, you have to wrap them in braces and use multi-line style comments.
<div className="dropdown">
{/* whenClicked is a property not an event, per se. */}
<Button whenClicked={this.handleClick} className="btn-default" title={this.props.title} subTitleClassName="caret"></Button>
<UnorderedList />
</div>
You can read more about how comments work in JSX here.
score:3
Conditional rendering
This approach mentioned on the React docs will also work with nested /**/
comments, unlike the {/**/}
approach, e.g.:
{false && <>
<div>
Commented out.
/* Anything goes. */
</div>
</>}
Full example:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello, World!</title>
<script src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.14.7/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
ReactDOM.render(
<div>
before
{false && <>
<div>
Commented out.
/* Anything goes. */
</div>
<div>
Also commented out.
/* Anything goes. */
</div>
</>}
after
</div>
,
document.getElementById('root')
);
</script>
</body>
</html>
renders just beforeafter
.
score:5
JavaScript comments in JSX get parsed as text and show up in your application.
You can’t just use HTML comments inside of JSX because it treats them as DOM nodes:
render() {
return (
<div>
<!-- This doesn't work! -->
</div>
)
}
JSX comments for single line and multiline comments follows the convention
Single line comment:
{/* A JSX comment */}
Multiline comments:
{/*
Multi
line
comment
*/}
score:6
According to React's Documentation, you can write comments in JSX like so:
One-line Comment:
<div>
{/* Comment goes here */}
Hello, {name}!
</div>
Multi-line Comments:
<div>
{/* It also works
for multi-line comments. */}
Hello, {name}!
</div>
score:7
{
// Any valid JavaScript expression
}
If you wonder why it works, it's because everything that's inside curly braces { } is a JavaScript expression.
So this is fine as well:
{ /*
Yet another JavaScript expression
*/ }
score:7
JSX Comments Syntax: You can use
{/**
your comment
in multiple lines
for documentation
**/}
or
{/*
your comment
in multiple lines
*/}
for multiple lines comment. And also,
{
//your comment
}
for single line comment.
Note: The syntax:
{ //your comment }
doesn't work. You need to type braces in new lines.
Curly braces are used to distinguish between JSX and JavaScript in a React component. Inside curly braces, we use JavaScript comment syntax.
Reference: click here
score:12
{/*
<Header />
<Content />
<MapList />
<HelloWorld />
*/}
score:13
Two ways to add comments in React Native
//
(double forward slash) is used to comment only single line in React Native code, but it can only be used outside of the render block. If you want to comment in a render block where we use JSX, you need to use the second method.If you want to comment something in JSX you need to use JavaScript comments inside of curly braces like {/* Comment here /}. It is a regular / Block comment */, but it needs to be wrapped in curly braces.
Shortcut keys for /* Block comments */:
Ctrl + / on Windows and Linux.
Cmd + / on macOS.
score:14
This is how.
Valid:
...
render() {
return (
<p>
{/* This is a comment, one line */}
{// This is a block
// yoohoo
// ...
}
{/* This is a block
yoohoo
...
*/
}
</p>
)
}
...
Invalid:
...
render() {
return (
<p>
{// This is not a comment! oops! }
{//
Invalid comment
//}
</p>
)
}
...
score:16
To summarize, JSX doesn't support comments, either html-like or js-like:
<div>
/* This will be rendered as text */
// as well as this
<!-- While this will cause compilation failure -->
</div>
and the only way to add comments "in" JSX is actually to escape into JS and comment in there:
<div>
{/* This won't be rendered */}
{// just be sure that your closing bracket is out of comment
}
</div>
if you don't want to make some nonsense like
<div style={{display:'none'}}>
actually, there are other stupid ways to add "comments"
but cluttering your DOM is not a good idea
</div>
Finally, if you do want to create a comment node via React, you have to go much fancier, check out this answer.
score:20
Besides the other answers, it's also possible to use single line comments just before and after the JSX begines or ends. Here is a complete summary:
Valid
(
// this is a valid comment
<div>
...
</div>
// this is also a valid comment
/* this is also valid */
)
If we were to use comments inside the JSX rendering logic:
(
<div>
{/* <h1>Valid comment</h1> */}
</div>
)
When declaring props single line comments can be used:
(
<div
className="content" /* valid comment */
onClick={() => {}} // valid comment
>
...
</div>
)
Invalid
When using single line or multiline comments inside the JSX without wrapping them in { }
, the comment will be rendered to the UI:
(
<div>
// invalid comment, renders in the UI
</div>
)
score:20
According to the official site, these are the two ways:
<div>
{/* Comment goes here */}
Hello, {name}!
</div>
Second example:
<div>
{/* It also works
for multi-line comments. */}
Hello, {name}!
</div>
Here is the reference: How can I write comments in JSX?
score:28
On the other hand, the following is a valid comment, pulled directly from a working application:
render () {
return <DeleteResourceButton
// Confirm
onDelete = {this.onDelete.bind(this)}
message = "This file will be deleted from the server."
/>
}
Apparantly, when inside the angle brackets of a JSX element, the //
syntax is valid, but the {/**/}
is invalid. The following breaks:
render () {
return <DeleteResourceButton
{/* Confirm */}
onDelete = {this.onDelete.bind(this)}
message = "This file will be deleted from the server."
/>
}
score:59
Here is another approach that allows you to use //
to include comments:
return (
<div>
<div>
{
// Your comment goes in here.
}
</div>
{
// Note that comments using this style must be wrapped in curly braces!
}
</div>
);
The catch here is you cannot include a one-line comment using this approach. For example, this does not work:
{// your comment cannot be like this}
because the closing bracket }
is considered to be part of the comment and is thus ignored, which throws an error.
Source: stackoverflow.com
Related Query
- How to use comments in React
- How to use componentWillMount() in React Hooks?
- How to use `setState` callback on react hooks
- How to use switch statement inside a React component?
- How to use refs in React with Typescript
- How to use callback with useState hook in react
- How to use children with React Stateless Functional Component in TypeScript?
- How can I make use of Error boundaries in functional React components?
- How to use throttle or debounce with React Hook?
- How to use onClick event on react Link component?
- react Material-ui, How do I know I can use onClick for button?
- How can I use React hooks in React classic `class` component?
- How to use clsx in React
- How to use generics in props in React in a functional component?
- React Formik : how to use custom onChange and onBlur
- How do I use external script that I add to react JS?
- How to use React Router with Electron?
- How to use Redux's Provider with React
- How to use jQuery UI with React JS
- How to use shouldComponentUpdate with React Hooks?
- How to use React without unsafe inline JavaScript/CSS code?
- How to use ReactDOM.createPortal() in React 16?
- How to use React useRef hook with typescript?
- How to use create-react-app with an older React version?
- How to use the increment operator in React
- How to use React Router with Laravel?
- How to use an array as option for react select component
- Making an HTML string from a React component in background, how to use the string by dangerouslySetInnerHTML in another React component
- React Native - how to use local SVG file (and color it)
- How to use absolute path to import custom scss, when using react + webpack?
More Query from same tag
- React HOC returns before async function can run
- Node.js effects in Thermite for Electron
- Cross Origin Resource Sharing Error: missingalloworiginheader in socket.io tweets streamer app
- How and where do I use the data from an Apollo Client?
- React's `memo` drops generics in the returned function
- What do I display until fetch is returned in React Redux
- How do you create edgeless graphql element in Gatsby?
- React route how to get to the page i want
- Passing props to React Router children routes
- Using react-router to redirect upon form submission
- How to reject fetch promise in redux
- Overriding Material-UI Select Style
- Issue with CKEditor 5 Easy Upload image in React filerepository-no-upload-adapter
- How to make responsive React bootstrap cards that are vertically aligned?
- React router provides javascript object instead of original type on url address bar reload
- ReactJs : Show or hide input fields based on select value
- Return error message in React
- Open Source React App Failed to Compile after npm install and npm start
- onChange or onBlur to change the state in ReactJs?
- Promise.all in Redux action and update state
- How to customize checkbox with JSS in React
- How to remove event listener manually in React
- React: Why child stopped re-render the 3rd times
- RelayContainer: Expected prop `%s` to be supplied to `%s`, but ' + 'got `undefined`. Pass an explicit `null` if this is intentional
- How to make a floating label which can move up when focus or has content in React?
- Relay Modern, prop not supplied
- What's wrong in my react redux application ,as it's not rendering things as expected?
- Rectangle selection on canvas inside React component on MouseMove event
- Add input validation to html input in React
- Svg path is not animating in React Spring animating library