why we use fragment in react code example
Example 1: react fragment
render() {
return (
);
}
Example 2: react fragment
render() {
return (
<>
>
);
}
Example 3: how to use react fragment
//the same way you'd use any other element
//except that it doesn't support keys or attributes.
render() {
return (
<>
Hello
World!
>
);
}
Example 4: why we use fragment in react
//when we are trying to render more than one root element we have to put the
//entire content inside the ‘div’ tag.
//Fragments to the rescue. Fragments are a modern syntax for adding multiple
//elements to a React Component without wrapping them in an extra DOM node.
//React Fragments do not produce any extra elements in the DOM, which means
//that a Fragment’s child components will be rendered without any wrapping DOM
//node.
//example:
function FragementDemo(){
return (
Hello world
This is a react fragment Demo
)
}
Related