简体   繁体   English

如何使用 useSelector 在功能组件中访问 redux 的 store state 变量?

[英]How to access redux's store state variable in a functional component using useSelector?

I have looked into multiple sources trying to solve this problem but could not find any answers.我已经研究了多个试图解决这个问题的来源,但找不到任何答案。 I have a functional component <Dashboard /> which will display some information from an API.我有一个功能组件<Dashboard /> ,它将显示来自 API 的一些信息。

I expected the component to first get into useEffect, execute the getData function and then display {devices} on the screen.我希望组件首先进入 useEffect,执行 getData function 然后在屏幕上显示 {devices}。 What happens, though, is that the store state is updated, but the component not.但是,发生的情况是存储 state 已更新,但组件未更新。 The {devices} variable is always undefined. {devices} 变量始终未定义。 I don't think I understand how to access my state variable from reducers/all/dashboard.js with useSelector.我想我不明白如何使用 useSelector 从 reducers/all/dashboard.js 访问我的 state 变量。

dashboard/index.js仪表板/index.js

import React, { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";

import api from "../../services/api";

import * as DashboardActions from "../../store/actions/dashboard";

const Dashboard = (props) => {
  const dispatch = useDispatch();
  const devices = useSelector(state => state.device)

  useEffect(() => {
    async function getData() {
      const pathname = "/dashboard";

      await api
        .get(pathname)
        .then((res) => {
          dispatch(DashboardActions.setData(res.data));
        })
        .catch((res) => {
          console.log(res.response.data);
        });
    }
    getData();
    console.log("devices ue ", devices);

  }, [dispatch]);

  return (
    <div>
      <h1>Dashboard</h1>
      <span>{devices}</span>
    </div>
  );
};

export default Dashboard;

reducers/all/dashboard.js减速器/所有/dashboard.js

const INITIAL_STATE = {
  devices: [],
};

function dashboard(state = INITIAL_STATE, action) {
  console.log("Action ", action)
  if ("DASHBOARD_SET_DATA" === action.type) {
    const data = action.data;
    console.log("Data: ", data.devices)
    state = { ...state, devices: data.devices };

    console.log("State ", state)
  }

  return state;
}

export default dashboard;

actions/dashboard.js动作/dashboard.js

export function setData(data) {
  return {
    type: "DASHBOARD_SET_DATA",
    data,
  };
}

I would appreciate any help a lot.我将不胜感激。

Thanks in advance!提前致谢!

The react-redux useSelector hook is selecting state from your redux store state object. The react-redux useSelector hook is selecting state from your redux store state object.

If your dashboard reducer is combined into your root reducer, something like如果您的dashboard减速器被组合到您的根减速器中,例如

const rootReducer = combineReducers({
  ... other reducers
  dashboard,
  ... other reducers
});

Then the devices state value should be accessed from state.dashboard.devices .然后设备 state 值应该从state.dashboard.devices访问。

The update for your component:您的组件的更新:

const devices = useSelector(state => state.dashboard.devices)

暂无
暂无

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

相关问题 如何在功能组件或 class 组件之外访问我的 redux state? - How to access my redux state outside of the functional component or class component? 在功能组件中,如何使用 react-redux 连接从 redux 商店访问道具? - In a Functional Component, how do you access props from the redux store using react-redux connect? 如何使用功能组件从 Redux 存储中检索数据并避免 useSelector 和 useDispatch - How to retrieve data from Redux store using functional components and avoiding useSelector & useDispatch 如何从功能组件访问 Redux 存储数据? - How to access Redux store data from a functional component? ReduxToolkit useSelector Hook:通过 useDispatch 更新选定的 redux-state 后,React 功能组件不会重新渲染 - ReduxToolkit useSelector Hook: React functional component doesnt rerender after selected redux-state is updated through useDispatch 在 redux 中调度操作后,如何使用 useSelector 访问状态的最新值? - How do I access the latest value of state using useSelector after dispatching an action in redux? 如何使用功能组件中的字符串访问状态变量? - How can I access a state variable using a string within functional component? 如何在 useEffect 的清理 function 中访问 state/redux 存储? - How to access state/redux store in the useEffect's cleanup function? 如何在功能组件中使用 onClick 正确更新 state 变量 - How to update a state variable correctly using onClick in functional component 如何将 redux state 变量设置为组件 state - How to set redux state variable to component state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM