React Flashcards
What is React?
a JavaScript library for creating user interfaces.
What is a React element?
Plain object describing a component instance or DOM node and its desired properties.
How do you mount a React element to the DOM?
ReactDOM.render(element, container, [callback]);
container is where you want to add the element.
What is JSX?
a syntax extension to JavaScript; produces React “elements”; fundamentally, JSX just provides syntactic sugar for the React.createElement(component, props, …children) function.
Why must the React object be imported when authoring JSX in a module?
Since JSX compiles into calls to React.createElement, the React library must also always be in scope from your JSX code.
Babel transform it into create call, will be undefined???
How can you make Webpack and Babel work together to convert JSX into valid JavaScript?
Babel compiles JSX down to React.createElement() calls.
Use babel-loader and @babel/plugin-transform-react-jsx packages.
What is a React component?
Takes input and returns a React element
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?
First letter has to be capitalized, has to return a react element
function Welcome(props) { return <h1>Hello, {props.name}</h1>; }
How do you mount a component to the DOM?
function Welcome(props) { return <h1>Hello, {props.name}</h1>; }
const element = <Welcome name=”Sara” />;
ReactDOM.render(
element, document.getElementById(‘root’)
);
What are props in React?
object passed into a React component
How do you pass props to a component?
const element = ( <ComponentName property-name="string" /> <ComponentName property-name={1 + 2} /> );
How do you write JavaScript expressions in JSX?
Inside curly brackets {}
How do you create “class” component in React?
To define a React component class, you need to extend React.Component. The only method you must define in a React.Component subclass is called render(). All the other methods described on this page are optional.
class Welcome extends React.Component { render() { return <h1>Hello, {this.props.name}</h1> } }
How do you access props in a class component?
use ‘this’ object
this.props.name
What is the purpose of state in React?
State is data model in react; used in render logic to determine what the user should see; to keep of track of values that change over time
State is similar to props, but it is private and fully controlled by the component.
Store property values belonging to component