Introduction to ReactJS
Intro
Following the ReactJS tutorial from the official react website. Won’t be listing the steps from the website here, just going to write down my thoughts and notes. React is a JAVASCRIPT LIBRARY used for building USER INTERFACES based around COMPONENTS.
Sidenote: const
variables can’t be reassigned and are consistent within the block its declared in. You can change properties of a const variable (like changing entries in a dictionary), but you can’t reassign the actual variable.
The Component Lifecycle
React components come with methods that are executed throughout their lifespan. These methods can/are meant to be override to give the components functionality. The catogories of lifecycle methods include ‘Mounting’, ‘Updating’, ‘Unmounting’, and ‘Error Handling’
Mounting
These methods are executed when an instance of a component is instantiated.
- constructor()
- static getDerivedStateFromProps()
- called before the render method during the mount and during any updates
- render()
- componentDidMount()
- best place to create network requests and/or any subscriptions
- calling setState() will still render before the browser screen is updated
Updating
These methods are called in order when re-rendering a component after a change to a prop or state.
- static getDerivedFromProps()
- shouldComponentUpdate()
- primarily used for performance optimizations (can ignore for now)
- render()
- getSnapshotBeforeUpdate()
- executed before rendered output is commited to the DOM. Commonly used for cases where scroll position in the DOM needs to be handled in a specific way.
- componentDidUpdate()
- best place to insert new network requests based on updated component data.
- should wrap network requests in a conditional statement to prevent any loops
Unmounting
- componentWillUnmount()
- perform any necessary cleanup here
Error Handling
- static getDerivedStateFromError()
- componentDidCath()
Other Component Methods/Properties
- setState()
- forceUpdate()
- forces component to re-render regardless of changes
- props
- state
After going through the tutorial and skimming through the docs, the next step is to now figure out how to connect a backend and/or make components from API calls. The most common way to make API calls is to use the native fetch API in JavaScript:
The fetch statement should be called in the higher order components and will propogate the data down to the actual components interested in the data.
Render Props
Render prop is a react pattern for sharing code between components.
- The Wrapper component contains the utility variables and methods it wants to supply, and returns them in its render return value
- The Higher Order Component (HOC) will will create a Wrapper component in the HOC’s render function and use the Wrapper’s exposed methods/variables and reference/use them in the HTML the HOC actually wants to return in the render function.
Here’s the best example I could find.
Starting a React App
Update npm and create a new react project:
curl -L https://www.npmjs.com/install.sh | sh
npm init react-app my-app
Run the app and/or build for production:
npm start
npm run build
Parent Children Communication
- use props to propogate data downward
- pass functions as props to children to propogate signals upward Using React.createElement to create a DOM element will render it to the DOM, but using React.createElement to create another React Component will not render the actual ‘Component’ element to the DOM, and instead render the output of its render function instead.