react 生命挂钩_如何在GraphQL API中使用React挂钩来管理状态
react 生命掛鉤
In this blog post, we are going to learn -
在這篇博客中,我們將學(xué)習(xí)-
Before we start working with hooks, let us take a brief moment to talk about state management.
在開始使用鉤子之前,讓我們花一點(diǎn)時(shí)間討論狀態(tài)管理。
State management is managing data that flows between our application components. It could be data flowing inside one component (local state) or data flowing between multiple components (shared state). We need to manage state because sometimes components need to talk to each other through a reliable source of information. In Redux, this reliable source of information is called the store.
狀態(tài)管理正在管理在我們的應(yīng)用程序組件之間流動(dòng)的數(shù)據(jù)。 它可以是在一個(gè)組件內(nèi)部流動(dòng)的數(shù)據(jù)(本地狀態(tài)),也可以是在多個(gè)組件之間流動(dòng)的數(shù)據(jù)(共享狀態(tài))。 我們需要管理狀態(tài),因?yàn)橛袝r(shí)組件需要通過(guò)可靠的信息源相互通信。 在Redux中,這種可靠的信息源稱為存儲(chǔ)。
第1部分:React鉤子-什么和為什么 (Part 1: React hooks - the what and why)
什么是鉤子? (What are hooks?)
Hooks are functions that lets you access state without using a class component. Hooks are a more natural way of thinking about React. Instead of thinking of what lifecycle methods to use, you can now write components thinking about how and when your data needs to be used.
掛鉤是使您無(wú)需使用類組件即可訪問(wèn)狀態(tài)的函數(shù)。 鉤子是思考React的一種更自然的方式。 現(xiàn)在,您無(wú)需考慮使用哪種生命周期方法,而可以編寫考慮如何以及何時(shí)使用數(shù)據(jù)的組件。
React hooks were introduced in October 2018 and released in February 2019. It is now available with React 16.8 and higher. React hooks became highly popular as soon as they were introduced.
React鉤子于2018年10月引入,并于2019年2月發(fā)布。它現(xiàn)在可用于React 16.8及更高版本。 引入后,React鉤子就變得非常流行。
為什么React鉤子如此受歡迎? (Why are React hooks so popular?)
More logical way of thinking of React: You no longer have to think about when React mounts a component and what you should do in componentDidMount and remember to clean it up in componentWillUnmount. Now you can think more directly about how data is consumed by your component. React takes care of handling the mounting and cleanup functions for you.
您再也不用去想陣營(yíng)坐騎當(dāng)一個(gè)組件,哪些是你應(yīng)該做的:發(fā)生React的思維更合乎邏輯的方式componentDidMount記得把它清理干凈的componentWillUnmount 。 現(xiàn)在,您可以更直接地考慮組件如何使用數(shù)據(jù)。 React會(huì)為您處理安裝和清理功能。
有哪些常見的鉤子? (What are some common hooks?)
1. useState (1. useState)
useState is used to set and update state like this.state in a class component.
useState用于在類組件中設(shè)置和更新類似this.state狀態(tài)。
const [ state, setState] = useState(initialState);2. useEffect (2. useEffect)
useEffect is used to carry out a function that does side effects. Side effects could include things like DOM manipulation, subscriptions, and API calls.
useEffect用于執(zhí)行產(chǎn)生副作用的功能。 副作用可能包括諸如DOM操作,訂閱和API調(diào)用之類的事情。
useEffect(() => {document.title = 'New Title' });3. useReducer (3. useReducer)
useReducer works similar to how a reducer works in Redux. useReducer is used when state is more complex. You can actually use useReducer for everything you do with useState. It gives a dispatch function in addition to a state variable.
useReducer的工作方式與Redux中reducer的工作方式類似。 當(dāng)狀態(tài)更復(fù)雜時(shí)使用useReducer。 實(shí)際上,您可以將useReducer用于useState的所有操作。 除了狀態(tài)變量外,它還提供了調(diào)度功能。
const [ state, dispatch ] = useReducer(reducer, initialArg, init);4. useContext (4. useContext)
useContext is similar to the context API. In the context API, there is a Provider method and Consumer method. Similarly, with useContext, the closest Provider method is used to read data.
useContext與上下文API相似。 在上下文API中,有一個(gè)Provider方法和Consumer方法。 類似地,對(duì)于useContext,最接近的Provider方法用于讀取數(shù)據(jù)。
const value = useContext(MyContext);For more detailed explanation of how each of these work, read the official docs.
有關(guān)這些功能的更多詳細(xì)說(shuō)明,請(qǐng)閱讀官方文檔 。
第2部分:如何使用掛鉤進(jìn)行狀態(tài)管理 (Part 2: How to use hooks for state management)
With React 16.8, you can use hooks out of the box.
使用React 16.8,您可以直接使用鉤子。
We are going to build an application to make a list of songs. Here is what it will do -
我們將構(gòu)建一個(gè)應(yīng)用程序來(lái)制作歌曲列表。 這是它的作用-
By the way, all the code is available on my GitHub. To see this in action, you can go to this CodeSandbox.
順便說(shuō)一下,所有代碼都可以在我的GitHub上找到 。 要查看實(shí)際效果,可以轉(zhuǎn)到此CodeSandbox 。
We are going to use the GraphQL API with this app, but you can do the following steps with a REST API as well.
我們將在此應(yīng)用程序中使用GraphQL API,但您也可以使用REST API執(zhí)行以下步驟。
步驟0:主要要點(diǎn) (Step 0: Main gist)
The main idea here is that we are going to use context to pass data down to our components. We will be using hooks, useContext and useReducer, to read and update this state. We will be using useState to store any local state. For doing side effects such as calling an API, we are going to use useEffect.
這里的主要思想是我們將使用context將數(shù)據(jù)向下傳遞到我們的組件。 我們將使用鉤子useContext和useReducer來(lái)讀取和更新此狀態(tài)。 我們將使用useState來(lái)存儲(chǔ)任何本地狀態(tài)。 為了產(chǎn)生諸如調(diào)用API的副作用,我們將使用useEffect 。
Let's get started!
讓我們開始吧!
步驟1:設(shè)定內(nèi)容 ( Step 1: Set up context)
import { createContext } from 'react';const Context = createContext({songs: [] });export default Context步驟2:初始化您的狀態(tài)。 稱這個(gè)initialState (Step 2: Initialize your state. Call this initialState)
We are going to use this context from to initialize our state:
我們將使用from的上下文來(lái)初始化我們的狀態(tài):
const initialState = useContext(Context);步驟3:使用useReducer設(shè)置reducers (Step 3: Setup reducers using useReducer)
Now we are going to set up reducers with an initialState with useReducer in App.js:
現(xiàn)在,我們將在App.js中使用useReducer設(shè)置帶有initialState的reduceors:
const [ state, dispatch ] = useReducer(reducer, initialState);步驟4:找出哪個(gè)是頂層組件。 (Step 4: Figure out which is the top level component.)
We will need to set up a Context Provider here. For our example, it will be App.js. Also, pass in the dispatch returned from useReducer here so that children can have access to dispatch:
我們將需要在此處設(shè)置一個(gè)上下文提供程序。 對(duì)于我們的示例,它將是App.js 另外,在此處傳遞useReducer返回的調(diào)度,以便子代可以訪問(wèn)調(diào)度:
<Context.Provider value={state,dispatch}>// children components<App /><Context.Provider value={state,dispatch}>步驟5:現(xiàn)在使用useEffect掛鉤連接API (Step 5: Now hook up the APIs using the useEffect hook)
const {state, dispatch} = useContext(Context);useEffect(() => {if(songs) {dispatch({type: "ADD_CONTENT", payload: songs});}}, [songs]);步驟6:更新狀態(tài) (Step 6: Update state)
You can use useContext and useReducer to receive and update global application state. For local state like form components, you can use useState.
您可以使用useContext和useReducer來(lái)接收和更新全局應(yīng)用程序狀態(tài)。 對(duì)于表單組件之類的本地狀態(tài),可以使用useState 。
const [artist, setArtist] = useState("");const [lyrics, setLyrics] = useState("");And that's it! State management is now set up.
就是這樣! 現(xiàn)在建立了狀態(tài)管理。
Did you learn anything new? Have something to share? Tweet me on Twitter.
你學(xué)到新東西了嗎? 有東西要分享? 在Twitter上發(fā)給我。
翻譯自: https://www.freecodecamp.org/news/hooks-and-graphql/
react 生命掛鉤
總結(jié)
以上是生活随笔為你收集整理的react 生命挂钩_如何在GraphQL API中使用React挂钩来管理状态的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 做梦梦到别人怎么回事
- 下一篇: 数字孪生营销_如何通过数字营销增加您的自