React Flashcards
Is React declarative or imperative?
It’s declarative. You say what you want to have happen rather than give step by step instructions.
What’s the syntax to create a new component called MyComponent?
Class MyComponent extends React.Component;
When is a component constructor function called?
Once, the first time the component is mounted (i.e. instantiated)
How do you change state inside a React component?
this.setState({ prop: value })
What are the two reasons to use a constructor in a component?
To initialize state or to bind event handlers to the class instance,
What are two requirements for a constructor function?
It should accept props as an argument and it should call super(props)
Does case matter in naming React components?
Yes, they must be uppercase or React will treat them as DOM tags
Difference between stateless and stateful components
Stateful are always class components, require constructor and set their state. Stateless functional components don’t have this, can’t use lifecycle functions like didMount;
Asynchronous setState() syntax?
this.setState((prevState, props) => ({
counter: prevState.counter + props.increment
}));
What’s wrong with this: this.handleClick(e)}>
A different callback is created with each render; if passed as a prop it may render twice. Use bind in constructor or use public class fields syntax.
Why is the index from an array not a good key for React?
If order changes, performance can suffer.
Why is data change without mutation important in React?
- Easier to undo/redo and time travel;
- Easier to track if changes have taken place to an object
- Performance: determining when to re-render.
What is a ‘controlled component’?
An input form where React controls state, e.g. input value = {this.state.value}.