Basics Flashcards
What are React elements?
Elements are what components are “made of”
Element —> <div></div>
What are components?
- Reusable pieces that make up UI
- Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.
- Such functions are called “pure” because they do not attempt to change their inputs, and always return the same result for the same inputs.
- Components must return a single root element.
Props
- When React sees an element representing a user-defined component, it passes JSX attributes to this component as a single object. We call this object “props”.
- React is pretty flexible but it has a single strict rule: All React components must act like pure functions with respect to their props
React is pretty flexible but it has a single strict rule, what is it?
All React components must act like pure functions with respect to their props.
What are pure functions?
They always return the same result for the same inputs.
How is state different from props?
State is similar to props, but it is private and fully controlled by the component.
What is an additional feature given to components defined as classes?
Local state is exactly that: a feature available only to classes.
What is a class constructor?
The constructor method is a special method for creating and initializing an object created with a class. There can only be one special method with the name “constructor” in a class. A SyntaxError will be thrown if the class contains more than one occurrence of a constructor method.
A constructor can use the super keyword to call the constructor of a parent class.
class Rectangle { constructor(height, width) { this.height = height; this.width = width; }
get area() { return this.calcArea(); }
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10, 10);
console.log(square.area);
Where is state defined?
In the class constructor
What is mount and unmount?
mount - is rendered to the DOM for the first time. This is called “mounting” in React.
unmount - whenever the DOM produced by the component is removed. This is called “unmounting” in React.
What are three things you should know about setState()
- Do Not Modify State Directly
For example, this will not re-render a component:
// Wrong this.state.comment = 'Hello'; Instead, use setState():
// Correct this.setState({comment: 'Hello'}); The only place where you can assign this.state is the constructor.
- State Updates May Be Asynchronous
React may batch multiple setState() calls into a single update for performance.
Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.
For example, this code may fail to update the counter:
// Wrong this.setState({ counter: this.state.counter + this.props.increment, }); To fix it, use a second form of setState() that accepts a function rather than an object. That function will receive the previous state as the first argument, and the props at the time the update is applied as the second argument:
// Correct this.setState((prevState, props) => ({ counter: prevState.counter + props.increment }));
State Updates are Merged
When you call setState(), React merges the object you provide into the current state.
For example, your state may contain several independent variables:
constructor(props) { super(props); this.state = { posts: [], comments: [] }; } Then you can update them independently with separate setState() calls:
componentDidMount() { fetchPosts().then(response => { this.setState({ posts: response.posts }); });
fetchComments().then(response => { this.setState({ comments: response.comments }); }); } The merging is shallow, so this.setState({comments}) leaves this.state.posts intact, but completely replaces this.state.comments.
When does componentDidMount() run?
after the component output has been rendered to the DOM.
If you don’t use something in render(), it should it be in the state?
No
Why state is often called local or encapsulated
It is not accessible to any component other than the one that owns and sets it.
What does this mean? –> import React from ‘react’
Import everything in the react module into the variable React