课程: React: Creating and Hosting a Full-Stack Site

免费学习该课程!

今天就开通帐号,24,700 门业界名师课程任您挑!

Creating a custom auth hook

Creating a custom auth hook

- [Instructor] Alright, so as I said, the next step here is going to be to create a custom React hook that we can use to very easily learn in any of our page components or other components such as a nav bar, whether the user is logged in or not, and if they are, what their email address is, as well as other information about the user. So here's how this is going to work. Creating custom hooks in React is actually pretty straightforward. We're just going to create a new file inside our source directory called useUser.js. This doesn't even need to be jsx because it's just going to be JavaScript code. And the first thing we're going to do is import a few utilities that we're going to need from both React and Firebase Auth. So here's what that's going to look like. We'll start off by saying import { useState, useEffect } from 'react'; we're going to use both of those hooks, and then we're going to say import { getAuth, onAuthStateChanged } another rather lengthy function name there from…

内容