English 中文(简体)
如何使用React Hook in Redux Saga?
原标题:How to use React Hook in Redux Saga?

我正在为我的用词。 但我有问题。

页: 1 但是,当我把它放在aga子中时,它就显示:

[Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.]

http://。 如果yes,how? 如果no,你可以提出解决办法。


最终解决办法是从沙迦传递信息。


这是我的传闻。 js file:

export function toastLoginSuccess() {
  ToastAndroid.show(
    LANGUAGES.toastLoginSuccess,
    ToastAndroid.SHORT,
    ToastAndroid.TOP,
  );
}

页: 1

www.un.org/Depts/DGACM/index_french.htm

I want change it to const LANGUAGES = React.useContext(LanguageContext).language;

而Saga是一份类似于:

export function* fetchSigninSaga(action) {
  try {
    // ...
    yield Toast.toastLoginSuccess())
  } catch (error) {
    // ...
    yield Toast.toastLoginFail();
  }
}
最佳回答

You need to inject your context in saga first, below is the way to achieve it,

import createSagaMiddleware from "redux-saga";

const userService = createUserService(...);

const sagaMiddleware = createSagaMiddleware({
    context: LanguageContext
});

sagaMiddleware.run(rootSaga);

一俟您投放,你就以以下方式使用:

import { getContext } from "redux-saga/effects";

export function* loadUsersSagas(action) {
    const languageContext = yield getContext("languageContext");
    const language = yield languageContext.getLanguage();
    ...
}

For more on this you can check, getContext

问题回答

如果您想使用 React Hook in the React Saga generator function(不仅仅是这一例子),你将不得不在saga generator function之外设立一个职能,然后在saga generator <>职能<>内指定职能。

const success = ()=> { 
   // your hook or any other hook 
   Toast.toastLoginSuccess()
}
const failed = ()=> { 
   // your hook or any other hook
   Toast.toastLoginFail()
}


然后,你可以称职。

export function* fetchSigninSaga(action) {
  try {
    // ...
    yield success()
  } catch (error) {
    // ...
    yield failed();
  }
}




相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...