Research optimising, and usage of React hooks #250
Labels
No labels
_CRITICAL_
API
app
backEnd
Blocked, waiting for further changes
bug
cleanup
close
design
duplicate
enhancement
feature request
frontEnd
help wanted
invalid
low priority
needs input
needs review
project documentation
question
research
reviewed
script
security
SQL
style
testing
topLevel
wontfix
No milestone
No project
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Andras/BoundlessFlowCampus2K#250
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Commonly used Hooks:
Medium
Rules of using Hooks:
Reason: The order in which Hooks are called is important. If Hooks are placed inside loops, conditionals, or nested functions, react is not able to keep track of the order. The result of having inconsistency with Hook calls are that the apps behavior can break which will result in bugs and performance issues.
Example:
Breaks the rule:
Correct way:
Reason: Hooks were made with the purpose of managing state and effects in React components. So if Hooks were used outside of React Functions their behavior would break their intended use. React would not be able to handle it.
Example:
Breaks the rule:
Correct Way:
Medium
What is a Hook?
A React Hook is a special function in React that lets developers use state and other React features without making a class. It makes code simpler and easier to manage by allowing functionality to be added directly within React components.
Medium
What is a Custom Hook?
Custom Hook is a JavaScript function that starts with "use". It allows a user to define hooks for specific purpose. For example a hook for fetching data. It usually combines one or more built in React Hooks. The custom hook would, for example be named "useFetch". One reason for using Custom Hook is to remove duplication of code.
Medium
How to create a Custom Hook:
Medium
Hooks let you use different React features from your components.
useState():
The useState function returns a pair [state-variable, setter]. The setter should always be used to change the state. State variables have two main functions:
useContext():
Allows components to retrieve data from parents far up the component tree. You can for example get the current theme from the root element without having to pass it down as a prop to each and every component.. Context is created by using the createContext() function.
useEffect():
Simplified a bit from here. There are two types of logic in a react component:
Unlike rendering code and event handlers which are only executed on rendering and user interaction, useEffect allows a function to be run after react is finished rendering. It's mostly used when 'synchronizing' with external systems (eg. fetching data from API) since it allows the website to be rendered before waiting on responses from external systems.
Are we creating this for the coding standard?
That is a very good idea since there are many ways to do the same thing in react, and I prefer that we choose a small subset to make the code more maintainable.
Advantages to using Hooks:
Simplicity and Readability: Using Hooks will lead to a more concise and easy to read code. This will then make it a better developer experience for those who are new to react.
Code Reusability: Using Hooks makes it easy to build reusable logic. For example by moving common tasks into Custom Hooks, we can use that Hook across multiple different components without repeating ourselves.
No Need for Classes: Hooks allow us to move away from class components entirely and allows for a purely functional approach. This also aligns with React best practices.
Medium
Disadvantages to using Hooks:
Compatibility Issues: Hooks were created in React 16.8 and there are some already existing codebases that are using heavily incorporated class components. Switching to Hooks might take a lot of effort, time and money.
Backward Compatibility: Hooks are not backward compatible with class components. This means that any migration from using class based code to Hooks will require a significant refactoring.
@hGustavs wrote in #250 (comment):
Yes
I've written a bit about react and how it works on the wiki. It's not quite done yet but some input would be nice. Some of the information is taken from #114
Should have a section explaining props. Right now you jump into an example "The Card function takes two props (parameters): { title, children }." but beginners might not know what props are.
Review of the React wiki
I have reviewed the official React documentation and compared it with this React wiki
The current wiki is overall correct and provides a good understanding of the topics shown. However there are some areas missing that would make the wiki more complete. As mentioned the JSX section is currently not there and must be added. The wiki could also be improved by adding a section explain prop and state and maybe the difference between them. While props are used as examples it is not fully explained. Another important topic in react that is missing is event handling for example, "onClick".
Overall, good wiki but missing some areas such as, JSX, prop and state, event handling.