What is an element in ReactJS
What is an element?
- As we have discussed, they form the basic block of the React app.
- They have the information that is displayed in UI.
Example:
const element = Hello, world
;
Here
.....
Hello world
.
Here is how we create and use an element in React.
const user = { name: 'Harry' }; const myElement = ( Hello, {user.name}!
); ReactDOM.render( myElement, document.getElementById('root') );
Nested Elements
- You can return only one element at a time in a component
- More than one element can be wrapped up inside a container element E.g. ...
- The following code is an example of Nested elements. You could see Hello! and Welcome..... are 2 elements nested within the element myMsg.
const myMsg = ( ); ReactDOM.render( myMsg, document.getElementById('root') );Hello!
Welcome to the React Tutorial on Fresco Play!
Rendering Elements
An element describes what you want to see on the screen.
const myMsg = Hello, world
;
Rendering an element into the DOM
Let us say, there is a All the javascript expressions can be used in JSX by enclosing the expressions inside curly braces. Here
root
will be managed by React DOM.Expressions in JSX
Example
Hello, {formatName(user)}!
);
ReactDOM.render(
element,
document.getElementById('root')
);
formatName(user)
is an expression and is enclosed within braces.