简体   繁体   English

我尝试将 React.createContext Api 注入 Thunk withExtraArgument 但不明白

[英]I try to inject React.createContext Api into Thunk withExtraArgument but don't understand something

I'm new to React learning and now I have this Redux Thunk that I need to inject this firebase Context into.我是 React 学习的新手,现在我有了这个 Redux Thunk,我需要将这个firebase Context 注入其中。

The FirebaseContext Context: FirebaseContext上下文:
(It also create an withFirebase that I use with Rect.Componets.) (这也创造withFirebase我跟Rect.Componets使用。)

    import React from 'react';
    
    const FirebaseContext = React.createContext(null);
    
    export const withFirebase = Component => props => (
        <FirebaseContext.Consumer>{firebase => <Component {...props} firebase={firebase} />}</FirebaseContext.Consumer>
    );

export default FirebaseContext;

This is how the FirebaseContext.Provider is assigned:这是 FirebaseContext.Provider 的分配方式:

import Firebase, { FirebaseContext } from './firebase';
import store from './redux/store';

ReactDOM.render(
    <FirebaseContext.Provider value={new Firebase()}>
        <Provider store={store}>
            <App />
        </Provider>
    </FirebaseContext.Provider>,

    document.getElementById('root'),
);

And creating the Redux store I inject it like this standars procedure:并创建 Redux 存储,我像这个标准程序一样注入它:

thunk.withExtraArgument(FirebaseContext),

And here is my Redux Thunk and when running this the Thunk is called but the FirebaseContext must be the value={new Firebase()} value but don't understand how to do that I have search for hours and also tried using the withFirebase to wrap the Thunk with but Thunk did not like that!这里是我的终极版咚并运行这个咚时被调用,但FirebaseContext必须是value={new Firebase()} value ,但不知道怎么做,我搜索了几个小时,也使用尝试withFirebase到将 Thunk 包裹起来,但 Thunk 不喜欢那样!

function doSomeWork() {
    return (dispatch, FirebaseContext) => {
        function work() {
            const firebase = FirebaseContext; // HERE IS THE PROBLEM 
            const userRef = firebase.userDoc(firebase.auth.currentUser.uid);
            dispatch(doSomeWorkStart());
            .....
        }
        return work;
    };
}

How can I pass the "value" that is the new Firebase() into my Thunk??如何将new Firebase()的“值”传递到我的 Thunk 中?

Edit:编辑:

Option 1:选项1:

I think instead of trying to pass the FirebaseContext down to your thunk, you should instead just pass the Firebase object itself.我认为与其尝试将 FirebaseContext 传递给您的 thunk,不如只传递 Firebase 对象本身。 You can't use context inside of thunk, so either you will need to pass the context value as an argument to your thunk from the component, or simply provide it when you configure the store.您不能在 thunk 内部使用上下文,因此您需要将上下文值作为参数从组件传递给您的 thunk,或者在配置商店时简单地提供它。

import Firebase, { FirebaseContext } from './firebase';
import reducer from './redux/reducer';

const firebase = new Firebase();
const store = createStore(
  reducer,
  applyMiddleware(thunk.withExtraArgument(firebase)),
);

ReactDOM.render(
  <FirebaseContext.Provider value={firebase}>
    <Provider store={store}>
      <App />
    </Provider>
  </FirebaseContext.Provider>,

  document.getElementById('root')
);

Also, you still need to correct the order of arguments in your thunk.此外,您仍然需要更正 thunk 中的参数顺序。 Redux-thunk will call your function with dispatch, getState, firebase in that order. Redux-thunk 会按照dispatch, getState, firebase的顺序调用你的函数。 Even though you aren't using getState you can't ignore it.即使您没有使用getState您也不能忽略它。 It will still be pass as an argument to your thunk.它仍然会作为参数传递给你的 thunk。

function doSomeWork() {
  return (dispatch, getState, firebase) => {
    function work() {
      const userRef = firebase.userDoc(firebase.auth.currentUser.uid);
      dispatch(doSomeWorkStart());
    }
    return work;
  };
}

Option 2:选项 2:

The alternative option, as mentioned, is to access the context inside of your component, and pass it as an argument to your action creator.如前所述,另一种选择是访问组件内部的上下文,并将其作为参数传递给动作创建者。

function doSomeWork(firebase) {
  return (dispatch, getState) => {
    function work() {
      const userRef = firebase.userDoc(firebase.auth.currentUser.uid);
      dispatch(doSomeWorkStart());
    }
    return work;
  };
}
function MyComponent(props) {
  const dispatch = useDispatch();
  const firebase = useContext(FirebaseContext);

  useEffect(() => {
    dispatch(doSomwWork(firebase))
  },[])

  return <></>;
}

However, I think the first option is your best bet.但是,我认为第一个选项是您最好的选择。

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

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