Thinking In React Flashcards
What is Step 1 in Thinking in React?
Break the UI into a component hierarchy
How should you approach breaking the UI into a component hierarchy?
- Draw boxes around every component and subcomponent in the mock you got from the designer
- SRP (single responsibility principle): A component should ideally do just one thing
What are the two types of “model” data in React?
- props (i.e., properties)
- state
What data type are both props and state in React?
Plain JavaScript objects
What is the primary difference between props and state in React?
Props get passed into a component (analogous to a function parameter)
while state is managed within the component only
What does it mean to say that props and state are deterministic in React?
The component should generate the exact same output given the same combination of props and state. If the component generates different output (given the above) something is broken.
- *When should you use state**
- *instead of a prop?**
If you will ever need to alter a component attribute at some point in time,
it should definitely go into the component’s state
What is a stateless component?
The component has props but no state.
What is a stateful component?
Both props and state; also known as state managers.
How do React achieve interactivity in components? Or, how can you trigger changes to your underlying data model?
With state
What three questions should one ask to determine if a piece of data should be in the component’s state?
- Is it passed in from a parent via props? (If so, it’s probably not state-worthy)
- Does it remain unchanged over time? (If so, it probably isn’t state)
- Can you compute it based on any other state or props in your component? If you can, it’s not state
- *What is the direction of**
- *data flow in React?**
React is all about one-way data flow down the component hierarchy.
Where should your state live in the context of multiple nested components?
It should live in a parent component that lives above all other components that require that state. If you can’t find a parent in which it makes sense to store the state, you may want to create a new component purely for holding the state.
What is JSX?
A syntax extension to JavaScript
An HTML proxy in JavaScript
JavaScript & XML
What does JSX do?
JSX produces React “elements.”