Reading-Notes

View the Project on GitHub MohammadAl-khatib/Reading-Notes

React2

Conditional Rendering

Conditions can be used to render elements depending on a state you want to check before rendering, this can be useful for login, log out, greeting,and preventing a componenet from rendering it can be done using the classic if statement or the && operator which executes the expression after it only if the expression before results in true.

Lists and Keys

map or looping generally, can be used to render multible elements, this can be useful when the components rendered holds data that is related to a list. When dealing with such approach it is a good practice to give each rendered component a unique key, this helps identifying each component, otherwise, a warning will appear and this may raise some issues later.

Forms

Forms are a way of collecting data from users, in HTML forms hold an internal state, it can be used in React, but letting the Ract states be the only source of truth is the better practice, this works fine with select, input, and textarea, those are all called controlled components. input type = "file" on the other hand is uncontrolled component and its value is a read-only.

Lifting State Up

Sometimes, a number of components share the same piece of data, and any change on data must be reflicted on all its common components, to do this, keep this data on the state of a common ancestor to the components, now all the components can access this piece of data and render accordingly.

Composition vs Inheritance

You can use props.children and pass elements as props to other components, this can be useful when the number of children is not specified early. Same happens when a component is a special case of another component, a set of props can be defined in the parent component, and part of them can be modified by a child component.

Thinking in React

  1. Break The UI Into A Component Hierarchy
  2. Build A Static Version in React
  3. Identify The Minimal (but complete) Representation Of UI State
  4. Identify Where Your State Should Live
  5. Add Inverse Data Flow