CodePamoja
CodePamoja means "Code Together" in Swahili. We are a group of passionate developers in The Netherlands and Kenya working together in virtual teams, transferring the latest software development skills to each other.
A JavaScript library for building user interfaces
The virtual DOM is a representation of the actual DOM. It's a pattern to change the actual DOM in a smart and fast way.
vs
From version 16.8.0 it doesn't really matter if you use a class or functional component, but it is recommended to write functional components.
Before version 16.8.0 it depends if you only need props or if you need the state, props and lifecycle methods.
Whether a component is a class or a function that uses Hooks is an implementation detail of that component. In the longer term, we expect Hooks to be the primary way people write React components. [source: https://reactjs.org/docs/hooks-faq.html#should-i-use-hooks-classes-or-a-mix-of-both]
We have no plans to deprecate classes. ..... But if the React community embraces Hooks, it doesn’t make sense to have two different recommended ways to write components. Hooks can cover all use cases for classes while providing more flexibility in extracting, testing, and reusing code. This is why Hooks represent our vision for the future of React. [source: https://medium.com/@dan_abramov/making-sense-of-react-hooks-fdbde8803889#f7df]
Since version 16.8.0 they are almost equal to class based components, so it doesn't matter.
Before version 16.8.0 if you only need props.
Since version 16.8.0 they are almost equal to functional components, so it doesn't matter.
Before version 16.8.0 if you need the state, props and lifecycle methods.
An element is the smallest building block in react. Components are created by one ore multiple elements.
This is the minimum what you need to start with React. A React element and an element inside the DOM to render your element.
React Elements are immutable. Once created, you can't change its children or attributes.
You can only update the UI to create a new element and pass it to the ReactDOM.render()
React only updates what is necessary by comparing what is changed in the desired state.
A React component is a collection of one or more elements.
Render a React Component in the DOM via ReactDOM.render()
It is recommended to make one App component as a wrapper for multiple child components.
Props are values that will be passed through components from outside. They are read-only and should not be modified inside the component!
If you want to do something with a prop value and modify it, use a pure function.
A pure function always respects it's inputs and will return always the same result for the same inputs.
Pure functions are testable!
All React components must act like pure functions with respect to their props.
A impure function will always return a different result because it will change one or more values that are brought into the function.
for functional components
With the useState method you can use and change the state of a functional component.
Hooks are introduced in React 16.8.0 and make it possible for functional components to access the state (and more).
The useEffect hook will be called during the mounting and destroying process
Pro's
Cons
for class components
Internal memory for data inside a class based component.
Use the state only when things will be updated later. If it does not need to be updated, just use the props to get the data via a parent component.
The state needs to be defined in the constructor.
Use this.setState() to update the local state of your component inside the componentDidMount event.
This will not trigger a re-render
This will trigger a re-render
React will batch multiple setState() calls into a single update for performance.
But since this.state and this.props are updated asynchronously, so we can't ensure to have them available at the same time.
a global state
A global state for data inside a React application, where every component can get their data from and send data to.
are only available in class based components
First method that will be called, before the mounting starts
If you don’t initialize state and you don’t bind methods, you don’t need to implement a constructor for your React component.
Call super(props) in order to have access to this.props
The only required method for class based components. Will perform the rendering of the component in the DOM.
A good place to:
Return the elements or content via JSX
Will be called when the component is rendered to the DOM
A good place to:
call setTimeout, setInterval, fetch and setState
Will be called when the state or props are updated
A good place to:
do network requests as long as you compare the current props to previous props (e.g. a network request may not be necessary if the props have not changed).
When the component is removed from the DOM
Is called before a component will be removed from the DOM
A good place use:
clearInterval, cancel subscriptions
And passing arguments to event handlers
For routing we use react-router-dom
By CodePamoja
Introduction into React training
CodePamoja means "Code Together" in Swahili. We are a group of passionate developers in The Netherlands and Kenya working together in virtual teams, transferring the latest software development skills to each other.