React.js Basics Flashcards
React.js Basics
When we render a list of components/elements, what property should we add to it?
key
The “key” property don’t get passed down to your components. What should we do if we need that value in our components?
We should explicitly pass the value as another prop
Which JS operators can be used to conditionally render components?
- The && operator (expression && component)
- Ternary operator
What is a “Controlled Component”?
An input form element whose value is controlled by React.
That is achieved by passing a value via the component’s props, and using that props as the value of the input:
Ex:
What are 3 DOM elements that have internal state?
- Input
- Textarea
- Select
To which event should you add a handler on a controlled component to handle mutation?
onChange
To which event should add a handler on a form to handle submission?
onSubmit
How is the “computed property name” ES6 syntax?
{ [prop] : value }
What happens if you specify the ‘value’ property when rendering a controlled component?
(eg: )
This will prevent the user to change the value of the component.
Explain the sentence: “The data always flows down”.
That means the state of a component can only be passed down to its children. A component can never pass state back to its parent.
How is the syntax for creating a new class component (with constructor and rendering methods)?
class Calculator extends React.Component { constructor(props) { super(props); } render() { return ( <p>Hello</p> ) } }
On the snippet:
return (<h1>MyValue</h1>);
Why is it recommended to put parentheses?
To avoid the pitfalls of JS’s automatic semicolon insertion.
What does Babel convert JSX expressions to?
To React.createElement() calls.
What is React’s equivalente to the following onclick event handling: Activate Lasers
Activate Lasers- Camel case event name- Function is passed as expression, not string
What is the difference between props and state for a component?
Props are properties that the component receives at creation time, and should be immutable.State are encapsulated properties that the component initializes (can be based on props values), and can mutate. State change causes component re-rendering.
What is the correct way to change a component’s state?
By calling this.setState()
In React what does “Lift the state up” means?
That is a technique to share state between components. Sharing state is accomplished by moving (lifting) it up to the closest common ancestor of the components that need it.The ancestor will then provide to the children both the state value and a handler function to mutate its state via the children props.
“There should be a single ‘source of truth’ for any data that changes in a React application.”“What does that quote mean in coding perspective?
Means that state should be owned by a single component. If the same state must be shared between components, it must be lifted up to an ancestral component.