简体   繁体   English

使用新的 React 钩子 useContext 的正确方法是什么?

[英]What is the right way to use new React hook useContext?

I have some difficulties to understand the new way to use react Context API.我很难理解使用 React Context API 的新方法。 I have an app with a custom class Firebase.我有一个带有自定义类 Firebase 的应用程序。 Now I want to make a hook to pass it.现在我想做一个钩子来传递它。 Before I used HOC (higher-order Component) and context.在我使用HOC (高阶组件)和上下文之前。

My questions我的问题

  1. Do I need to use HOC or it's a new way to do this?我需要使用 HOC 还是这是一种新的方法?
  2. Do I need the Context.Provider or it's new Hook?我需要 Context.Provider 还是它的新 Hook?
  3. Do I need to declare default value as a null or I can pass my Object right from context.js我是否需要将默认值声明为 null 或者我可以直接从 context.js 传递我的对象
  4. How can I use a new Hook instead of HOC in mine code?如何在我的代码中使用新的 Hook 而不是 HOC?

Here is my code with some comments related to questions这是我的代码,其中包含一些与问题相关的评论

// context.js this is my hoc
// index.jsx
import App from './App'
import Firebase, { FirebaseContext } from './components/Firebase'

const FirebaseContext = React.createContext(null)

export const withFirebase = Component => (props) => {
  // I don't need to wrap it to the FirebaseContext.Consumer
  // 1 But do I need this HOC or it's a new way?
  const firebase = useContext(FirebaseContext)
  return <Component {...props} firebase={firebase} />
}

ReactDOM.render(
  // 2 Here I'm lost. Do I need the FirebaseContext.Provider or not?
  // 3 Do I need to declare value her or I should do it in context.js as a default?
  <FirebaseContext.Provider value={new Firebase()}>
    <App />
  </FirebaseContext.Provider>,
  document.getElementById('root'),
)

// App.jsx
// 4 Can I use a new Hook instead of HOC here and how?
import { withFirebase } from './components/Firebase/context'
const App = () => {
    const firebase = this.props.firebase // But should be useContext(FirebaseContext) or something like this?
    return(...)
}
export default withFirebase(App) // I don't need this with the Hook

Any help appreciated.任何帮助表示赞赏。

You should understand it first that, useContext is just to make use of Context and acts like a consumer and not Provider. 您首先应该理解, useContext只是用于使用Context并且像消费者而不是提供者。

To answer your questions 回答你的问题

Do I need to use HOC or it's a new way to do this? 我是否需要使用HOC或者这是一种新方法?

You don't need an HOC with hooks. 你不需要带挂钩的HOC。 Hooks are meant to replace HOCs and render props pattern. 钩子意味着取代HOC并渲染道具图案。

Do I need the Context.Provider or it's new Hook? 我需要Context.Provider还是新的Hook?

There is no hooks equivalent of Context.Provider. 没有与Context.Provider相当的钩子。 You have to use it as is. 你必须按原样使用它。

Do I need to declare default value as a null or I can pass my Object right from context.js 我是否需要将默认值声明为null,或者我可以从context.js直接传递我的Object

The default value to createContext is only used if you don't pass a value props to the Context.Provider . 仅当您没有将value props传递给Context.Provider才使用createContext的默认值。 If you pass it the default value is ignored. 如果您传递它,则忽略默认值。

How can I use a new Hook instead of HOC in mine code? 如何在我的代码中使用新的Hook而不是HOC?

Instead of using useContext in the component returned by HOC use it directly within the component 而不是在HOC返回的组件中使用useContext ,而是直接在组件中使用它

Sample code 示例代码

/ context.js this is my hoc
// index.jsx
import App from './App'
import Firebase, { FirebaseContext } from './components/Firebase'

const FirebaseContext = React.createContext(null)

ReactDOM.render(
  <FirebaseContext.Provider value={new Firebase()}>
    <App />
  </FirebaseContext.Provider>,
  document.getElementById('root'),
)

App.jsx App.jsx

const App = () => {
    const firebase = useContext(FirebaseContext) 
    return(...)
}
export default App;
  1. Do I need to use HOC or it's a new way to do this?我需要使用 HOC 还是这是一种新的方法?

No, you don't need to use HOC as best technique.不,您不需要将 HOC 用作最佳技术。

Why?为什么? Starting from React v7.0 , you can use functional-based components.React v7.0开始,您可以使用基于函数的组件。 From this version efficient is to use the the latest technique named HOOKS, which were designed to replace class and provide another great alternative to compose behavior into your components.从这个版本开始,高效的是使用名为 HOOKS 的最新技术,该技术旨在替换类并提供另一种将行为组合到组件中的绝佳替代方案。


  1. Do I need the Context.Provider or it's new Hook?我需要 Context.Provider 还是它的新 Hook?

Hook like useContext() has a relation with Context.Provider.像 useContext() 这样的 Hook 与 Context.Provider 有关系。
Context is designed to share data that can be considered “global”.上下文旨在共享可被视为“全局”的数据。

The Provider component accepts a value prop to be passed. Provider组件接受要传递的 value prop。 Every Context come with a Provider.每个上下文都带有一个提供者。

Context.Provider component available on the context instance is used to provide the context to its child components, no matter how deep they are.上下文实例上可用的Context.Provider组件用于为其子组件提供上下文,无论它们有多深。


  1. Do I need to declare default value as a null or I can pass my Object right from context.js?我是否需要将默认值声明为 null 或者我可以直接从 context.js 传递我的对象?

No, you don't need necessarily to declare a default value.不,您不必声明默认值。

Example of defining the context in one corner of the codebase without defaultValue.在代码库的一个角落定义上下文而不使用 defaultValue 的示例。

const CountStateContext = React.createContext() // <-- define the context without defaultValue const CountStateContext = React.createContext() // <-- 定义没有 defaultValue 的上下文


  1. How can I use a new Hook instead of HOC in mine code?如何在我的代码中使用新的 Hook 而不是 HOC?

index.jsx索引.jsx

import App from './App'

import Firebase, { FirebaseContext } from './components/Firebase'

const FirebaseContext = React.createContext(null)

ReactDOM.render(
  <FirebaseContext.Provider value={new Firebase()}>
    <App />
  </FirebaseContext.Provider>,
  document.getElementById('root'),
)

Root Component: App.js, where will be used data comes form context:根组件:App.js,将使用的数据来自上下文:

const App = () => {
    const firebase = useContext(FirebaseContext) 
    return(...)
}
export default App;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM