React Flashcards
What is React?
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
How do you mount a React element to the DOM?
const create = React.createElement(
‘h1’,
null,
‘Hello, React!’
);
const container = document.querySelector(‘#root’);
ReactDOM.createRoot(container).render(create);
What is a React component?
a React component is a JavaScript function that you can sprinkle with markup.
How do you define a function component in React?
With function Profile() { } you define a JavaScript function with the name Profile.
How do you mount a component to the DOM?
const container = document.querySelector(‘#root’);
const root = ReactDOM.createRoot(container);
root.render(variable);
What are props in React?
standardized way to pass arguments
How do you pass props to a component?
list properties
How do you write JavaScript expressions in JSX?
curly brackets
What is the purpose of state in React?
A state variable to retain the data between renders.
A state setter function to update the variable and trigger React to render the component again.
How to you pass an event handler to a React element?
<button onClick={handleClick}>{props.text}</button>;
What are controlled components?
An input like <input></input> is uncontrolled. Even if you pass an initial value like <input></input>, your JSX only specifies the initial value. It does not control what the value should be right now.
What two props must you pass to an input for it to be “controlled”?
value, onChange
What Array method is commonly used to create a list of React elements?
filter() and map()
What is the best value to use as a “key” prop when rendering lists?
something unique (don’t use array index - order may change)
Data from a database: If your data is coming from a database, you can use the database keys/IDs, which are unique by nature.
Locally generated data: If your data is generated and persisted locally (e.g. notes in a note-taking app), use an incrementing counter, crypto.randomUUID() or a package like uuid when creating items.