1 Hook (useXXXX), whether custom, or basic built-in, are incomplete, abstract general R components; Used to add functionality to other R components;

2 RV component is only one of R component, R component encapsulates the browser behavior JS object, can be expressed by JSX 2021-08-22

Problem: Implement front-end UA module

Answer:

The login(logout) page is used to login to a node as a root node. The login(logout) page is also used to login to a node as a root node. And private pages using 3 related pages using this context(using useContext hook), Login E private Page Use auth.user L how to define private page? Private M (Private API)Copy the code

Hooks: Custom hook authoring is used to add additional (but generic) formal functionality to some RV components

UA: 1) user (credantial) creation, 2) login authentication, and 3) resource access authentication can be separated

Router: Add navigation and load logic to the RV component (via a route state)

UA implementation module composition

1 App. Js | @ 1 UA the foundations of the module, Global data 2 private page (for example Navbar. Js) | @ 3 login. 2, 3) access to resources validation js | @ 3 2) login authentication use 4 - auth. Js | this is the most main custom hook to achieveCopy the code

4 use – auth. Js | this is the most main custom hook to achieve

// use-auth.js is a hybrid module, Import React, {useState, useEffect, useContext, createContext} from "React "; // This global context const authContext = createContext(); Export const useAuth = () => {return useContext(authContext); }; // This is an API for attaching auth objects, used for @1 // is itself a RV component (available on JSX), Export function ProvideAuth({children}) {const auth = useProvideAuth(); } // 1 this is a hook, and it has state and useEffect. Function useProvideAuth() {const [user, setUser] = useState(null); const signin = (email, password) => {}; const signup = (email, password) => {}; const signout = () => {}; const sendPasswordResetEmail = (email) => {}; const confirmPasswordReset = (code, password) => { }; useEffect(() => {}, []); // Return the user object and auth methods return { user, signin, signup, signout, sendPasswordResetEmail, confirmPasswordReset, }; }Copy the code

Out of code

Beyond reading code, what capabilities should we have for migration

2. Use the React Context API to solve the task of global state collaboration (this example is authentication state, L Hook is an incomplete, not specific RV component. Do not forget this example, useProvideAuth, useAuth are custom hooks. L useProvideAuth Select the AUth object to the Provider node L Data Provider mode, UseAuth check authContext for private Page. 4 Use Children Prop to create RVX components for parent-child relationshipsCopy the code

Among these, the knowledge transfer of the third custom hook is the most difficult and valuable

The most difficult reason is the lack of knowledge about what the RV component should be able to extract. For example, it's not easy for you to understand that the data provider's ProvideAuth is also a RV component. The reason is, first, It checks the useProvideAuth function and returns JSX E if the VDOM output is the RV component, then ProvideAuth is an R component without its own VDOM output (subcomponents do not count, it is a template); JSX can be used to express the L R component is a browser behavior JS object; The most common type of R component with DOM output behaviorCopy the code