React Flashcards
What is React?
React is a JavaScript library for building user interfaces.
What is a React element?
It is the basic building block in a react application, it is an object representation of a virtual DOM node. React Element contains both type and property. It may contain other Elements in its props. React Element does not have any methods, making it light and faster to render than components.
Data type is an object that essentially describes a DOM element
How do you mount a React element to the DOM?
You can tell React to “mount” it into a DOM container by calling: ReactDOM.
What is JSX?
It is a syntax extension to JavaScript. JSX stands for JavaScript XML. JSX allows us to write HTML in React. JSX makes it easier to write and add HTML in React.
Why must the React object be imported when authoring JSX in a module?
The JSX gets internally into many React.createElement() function calls. Now because of this, we need to import React from “react” since internally every JSX is creating a React Component using JSX transformer.
So, for using the createElement function we need to import React and if we do not import it then React.createElement function will be undefined.
Syntactic sugar
const element = <h1>Hello, JSX!</h1>
turns into
const element = React.createElement(‘h1’, null, ‘Hello, JSX!’)
How can you make Webpack and Babel work together to convert JSX into valid JavaScript?
the babel loader, install the plugin that transforms react into JSX
What is a React component?
Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.
Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.
How do you define a function component in React?
JavaScript Function
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
or ES6 class
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
How do you mount a component to the DOM?
import React from ‘react’;
import ReactDOM from ‘react-dom/client’;
function CustomButton( ) {
return <button>Click me!</button>;
}
const divRoot = document.getElementById(‘root’);
const root = ReactDOM.createRoot(divRoot);
root.render(<CustomButton></CustomButton>);
What are props in React?
In ReactJS, the props are a type of object where the value of attributes of a tag is stored. The word “props” implies “properties”, and its working functionality is quite similar to HTML attributes. Basically, these props components are read-only components.
How do you pass props to a component?
Props is a key value pair that looks like an attribute: ‘text=”i”
i.e.
const element = (
<div>
<CustomButton></CustomButton>
<CustomButton></CustomButton>
<CustomButton></CustomButton>
</div>
);
React components use props to communicate with each other. Every parent component can pass some information to its child components by giving them props. Props might remind you of HTML attributes, but you can pass any JavaScript value through them, including objects, arrays, and functions.
First, pass some props to Avatar. For example, let’s pass two props: person (an object), and size (a number):
export default function Profile() {
return (
<Avatar
person={{ name: ‘Lin Lanying’, imageId: ‘1bX5QH6’ }}
size={100}
/>
);
}
If double curly braces after person= confuse you, remember they are merely an object inside the JSX curlies.
Now you can read these props inside the Avatar component.
How do you write JavaScript expressions in JSX?
To add JavaScript code inside JSX, we need to write it in curly brackets like this:
const App = () => {
const number = 10;
return (
<div>
<p>Number: {number}</p>
</div>
);
};
Here’s a Code Sandbox Demo.
Inside the curly brackets we can only write an expression that evaluates to some value.
How do you create “class” component in React?
class Car extends React.Component {
render() {
return <h2>Hi, I am a Car!</h2>;
}
}
How do you access props in a class component?
this.props
const element = <h1>Hello, JSX</h1>
React element of type h1 with a child of Hello, JSX, being assigned to const element