React.js Flashcards
What is React?
A front-end framework. It lets you use reusable components and makes it easier to implement interactive elements.
What is a React element?
A custom written DOM element. React apps are made out of components. A component is a piece of the UI (user interface) that has its own logic and appearance. A component can be as small as a button, or as large as an entire page.
How do you mount a React element to the DOM?
What is JSX?
How does React use JSX to render components?
What is a React component?
How do you define a component in React?
How do you mount a component to the DOM (or “render” a component)?
What are props in React?
The react equivalent of HTML attributes.
How do you use props in a component?
Write the name of the props inside curly braces { prop1, prop2 }. Destructure it.
How do you pass props to a component?
How do you write JavaScript expressions in JSX?
Within curly braces { }.
How do you pass an event handler to a React component?
You pass the prop for the event handler with a value of the event handler prop.
onXxx={handleXxx}
What is the naming convention for event handlers?
React documentation suggests that Event Handlers be named handleXxx, where Xxx is the event it is handling. This is just a convention, but it aids code clarity.
What are some custom event handler props a component may wish to define?
onClick, onSubmit, onChange, onMouseEnter,
What is the naming convention for custom event handler props?
React documentation suggests that Event Handler props be named onXxx, where Xxx is the event. This is just a convention, but it aids code clarity.
What are hooks in React?
Hooks are special functions that are only available while React is rendering. They let you “hook into” different React features. They all start with “use” followed by an uppercase letter.
What are the “Rules of Hooks”?
Only call hooks at the top level, the first line after your react function and cannot be inside loops, conditions, or nested functions.
Only call hooks from react components or from other hooks.
Must start with “use” then an uppercase letter e.g. “useHook”
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.
Why can’t we just maintain state in a local variable?
- Local variables don’t persist between renders. When React renders this component a second time, it renders it from scratch—it doesn’t consider any changes to the local variables.
- Changes to local variables won’t trigger renders. React doesn’t realize it needs to render the component again with the new data.
What two actions happen when you call a state setter function?
- It renders the component again.
- Remembers the latest state value.
When does the local state variable get updated with the new value?
The next time the component would render. On the re-render. The next time useState is called.
How do controlled components differ from uncontrolled components?
Controlled components are controlled by the parent, e.g. the form while uncontrolled components are controlled by the child, e.g. the input element.
What are some advantages of using uncontrolled components?
You don’t have to keep track of state with useState and just get the form data instead of each state variable.