*Readings: Putting it all together
React Docs - Thinking in React
- What is the single responsibility principle and how does it apply to components? One such technique is the single responsibility principle, that is, a component should ideally only do one thing. If it ends up growing, it should be decomposed into smaller subcomponents.
- What does it mean to build a ‘static’ version of your application? A version that takes your data model and renders the UI but has no interactivity.
- Once you have a static application, what do you need to add? Components that reuse other components and pass data using props.
- What are the three questions you can ask to determine if something is state? Is it passed in from a parent via props? If so, it probably isn’t state. 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 so, it isn’t state.
- How can you identify where state needs to live? Identify every component that renders something based on that state.
Find a common owner component (a single component above all the components that need the state in the hierarchy).
Either the common owner or another component higher up in the hierarchy should own the state.
If you can’t find a component where it makes sense to own the state, create a new component solely for holding the state and add it somewhere in the hierarchy above the common owner component.
Higher-Order Functions
- What is a “higher-order function”? Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions.
- Explore the greaterThan function as defined in the reading. In your own words, what is line 2 of this function doing? The greater than function is a function thats creates new functions. Returning whether the value of m is greater than n.
- Explain how either map or reduce operates, with regards to higher-order functions. The map method transforms an array by applying a function to all of its elements and building a new array from the returned values. The new array will have the same length as the input array, but its content will have been mapped to a new form by the function. The higher-order operation that represents this pattern is called reduce (sometimes also called fold). It builds a value by repeatedly taking a single element from the array and combining it with the current value. When summing numbers, you’d start with the number zero and, for each element, add that to the sum.
References
https://reactjs.org/docs/thinking-in-react.html
https://eloquentjavascript.net/05_higher_order.html#h_xxCc98lOBK