DEV Community

Phinehas Alabi
Phinehas Alabi

Posted on

React for Beginners

React is JavaScript Library used for building Rich UI and helps facilitate page interactions.

some best practices in react:

  • you may want to close tags /> if they are empty. ex.
const output= <img src={user.someUrl} />;
Enter fullscreen mode Exit fullscreen mode

keywords used throughout this post:

  • attr - attribute
  • output - displays the desired result
  • UI - user interface

Introducing JSX in React -
React JSX: Allows us to put HTML into JavaScript.
ex:

const firstName = "Oteele"
Enter fullscreen mode Exit fullscreen mode

in our html file, we can refer to the variable name firstName in an H1 tag like below:

<h1>my name is {firstName}</h1> // Oteele 
Enter fullscreen mode Exit fullscreen mode

*OR try the below in your IDE of choice: *

function personName(user) {
  return user.firstName + ' ' + user.lastName;
}
const user = {
  firstName: 'Oteele',
  lastName: 'Ankasa'
};
const output= (
  <h1>
    Hello, {personName(user)}!
  </h1>
);
Enter fullscreen mode Exit fullscreen mode

We can also specify attributes with JSX:
using quotes to specify string literals as attr.

const output= <a href="https://www.reactjs.org"> link </a>;
Enter fullscreen mode Exit fullscreen mode

using curly braces to embed a JSX in an atrr.

const output= <img src={user.someUrl}></img>;
Enter fullscreen mode Exit fullscreen mode

As a side note: Passing double quotes around a curly braces when embedding JSX in an attribute would not wok as expected."{}" ❌.
Better to ignore adding double quotes. {} ✅

Top comments (0)