React.js Flashcards
What is React?
A JavaScript library for building user interfaces
What is a React element?
An element created in react.
Ex:
const $h1 = React.createElement( 'h1', null, 'Hello, React!' );
How do you mount a React element to the DOM?
ReactDOM.render( )
Ex:
ReactDOM.render($h1, $root);
What is JSX?
JSX is an inline markup that looks like HTML and gets transformed to JavaScript. A JSX expression starts with an HTML-like open tag, and ends with the corresponding closing tag. JSX tags support the XML self close syntax so you can optionally leave the closing tag off.
JavaScript Syntax Extension
Why must the React object be imported when authoring JSX in a module?
So the JSX can be converted into valid JavaScript that a browser can understand.
How can you make Webpack and Babel work together to convert JSX into valid JavaScript?
Use/install babel-loader. (devDependencies)
It needs this for webpack to convert the JSX to JavaScript and throw it into main.js.
Ex:
module.exports = {
resolve: {
extensions: [‘.js’, ‘.jsx’]
},
module: {
rules: [
{
test: /.jsx?$/,
use: {
loader: ‘babel-loader’,
options: {
plugins: [
‘@babel/plugin-transform-react-jsx’
]
}
}
}
]
},
performance: {
hints: false
}
};
What is a React component?
Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.
Purpose is to return React elements.
How do you define a function component in React?
function Welcome(props) { return <h1>Hello, {props.name}</h1>; }
const element = ; ReactDOM.render( element, document.getElementById('root') );
How do you mount a component to the DOM?
function CustomButton(props) { return Click Me!; }
const $root = document.querySelector(‘#root’);
ReactDOM.render(, $root);
What are props in React?
“Props” is a special keyword in React, which stands for properties and is being used for passing data from one component to another. Furthermore, props data is read-only, which means that data coming from the parent should not be changed by child components.
How do you pass props to a component?
insert it directly into the element
How do you write JavaScript expressions in JSX?
return {props.text};
How do you create “class” component in React?
class ClassName extends React.Component{ render( ) { return Sample }
How do you access props in a class component?
Getting it from “this” object
What is the purpose of state in React?
The state is a built-in React object that is used to contain data or information about the component. A component’s state can change over time; whenever it changes, the component re-renders. The change in state can happen as a response to user action or system-generated events and these changes determine the behavior of the component and how it will render.
Tells the component what to render.
State is a data-model.