@abdulghani/reduxcontext 中文文档教程

发布于 5年前 浏览 21 更新于 3年前

Redux Context setup

在这个项目中有几个组件你可以导入到你的项目中使用

  • AppContextProvider default providing context to it's children and specify reducer to use
  • useSelector named function for selecting/get the state value from the context state
  • useDispatch named function for dispatching actions to your context's reducer

Usage

你可以这样使用它

  // import your deps
  import AppContextProvider from "@abdulghani/reduxcontext";
  import myReducer from "somewhere";

  const App = () => {
    return (
      <AppContextProvider reducer={myReducer}>
        <MyApp/>
      </AppContextProvider>
    )
  }

你可以通过这种方式从你的组件访问状态

  // other imports
  import {useSelector, useDispatch} from "@abdulghani/reduxcontext"

  const MyView = () => {
    const state = useSelector(state => state);
    const dispatch = useDispatch();

    useEffect(() => {
      // do something in initial render
      dispatch({ type: "MY_ACTION", data: "SOME DATA" });
    }, [])
  }

Redux Context setup

In this project there's several component you could import to your project to use

  • AppContextProvider default providing context to it's children and specify reducer to use
  • useSelector named function for selecting/get the state value from the context state
  • useDispatch named function for dispatching actions to your context's reducer

Usage

You can use it this way

  // import your deps
  import AppContextProvider from "@abdulghani/reduxcontext";
  import myReducer from "somewhere";

  const App = () => {
    return (
      <AppContextProvider reducer={myReducer}>
        <MyApp/>
      </AppContextProvider>
    )
  }

And you can access the state from your component this way

  // other imports
  import {useSelector, useDispatch} from "@abdulghani/reduxcontext"

  const MyView = () => {
    const state = useSelector(state => state);
    const dispatch = useDispatch();

    useEffect(() => {
      // do something in initial render
      dispatch({ type: "MY_ACTION", data: "SOME DATA" });
    }, [])
  }
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文