英文:
How to get the value of state from redux when the component is opened in anothe browser tab?
问题
目前我正在尝试在我的应用程序中实现Redux Toolkit,并在那里创建了存储并将值设置为状态。
但问题是,当我在新标签中打开组件时,我无法访问存储在Redux状态中的值。请建议如何解决这个问题。
以下是供参考的代码:
intex.tsx
import { createRoot } from 'react-dom/client';
import App from './App';
import './styles/css/index.css';
import { Provider } from 'react-redux';
import { store } from './util/redux/store';
const container = document.getElementById('root');
const root = createRoot(container!);
root.render(
<Provider store={store}>
<App />
</Provider>,
);
module.hot?.accept();
gridRowSlice.tsx
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { RootState } from './store';
export interface GridState {
activeRow: any;
}
const initialState: GridState = {
activeRow: {},
};
export const dataSlice = createSlice({
name: 'gridRow',
initialState,
reducers: {
move: (state, action) => {
state.activeRow = action.payload;
},
},
});
export const { move } = dataSlice.actions;
export const movedData = (state: RootState) => state.stocksList.activeRow;
export default dataSlice.reducer;
hooks.tsx
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import type { RootState, AppDispatch } from './store';
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
store.tsx
import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit';
import gridRowReducer from './gridRowSlice';
export const store = configureStore({
reducer: {
stocksList: gridRowReducer,
},
});
export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof store.getState>;
export type AppThunk<ReturnType = void> = ThunkAction<ReturnType, RootState, unknown, Action<string>>;
在这个组件中,我使用dispatch从表格发送一些数据。
import { move } from '../../util/redux/gridRowSlice';
import { useAppDispatch } from '../../util/redux/hooks';
const table = () => {
useEffect(() => {
dispatch(move(tableData));
});
return (
<div>
//表格代码
</div>
);
};
export default table;
在其他组件中,我想访问更新状态的值,尝试了不同的方法,但都不适用于我。这是我尝试的方法。
import { store } from '../util/redux/store';
import { useAppSelector } from '../util/redux/hooks';
const receiveData = () => {
const data = useAppSelector(movedData);
const view = store.getState();
const view2 = useAppSelector((state) => state.stocksList.activeRow);
useEffect(() => {
console.log(store.getState(), 'data1'); // 返回空对象
console.log(data, 'data2'); // 返回空对象
console.log(view, 'data3'); // 返回空对象
console.log(view2, 'data4'); // 返回空对象
});
return (
<div>
//代码
</div>
);
};
export default receiveData;
注意:我希望在应用程序正在运行时,无论在哪个标签中打开,都能获取状态的值。
英文:
Currently I'm trying to implement the redux toolkit in my application and there i have created the store and set the value to the state.
But the issue is I'm unable to access the value stored in the redux state wen I open the component in new tab. So how it can be done please suggest.
Here is the code for the reference:-
intex.tsx
import { createRoot } from 'react-dom/client';
import App from './App';
import './styles/css/index.css';
import { Provider } from 'react-redux';
import { store } from './util/redux/store';
const container = document.getElementById('root');
const root = createRoot(container!);
root.render(
<Provider store={store}>
<App />
</Provider>,
);
module.hot?.accept();
gridRowSlice.tsx
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { RootState } from './store';
export interface GridState {
activeRow: any;
}
const initialState: GridState = {
activeRow: {},
};
export const dataSlice = createSlice({
name: 'gridRow',
initialState,
reducers: {
move: (state, action) => {
state.activeRow = action.payload;
},
},
});
export const { move } = dataSlice.actions;
export const movedData = (state: RootState) => state.stocksList.activeRow;
export default dataSlice.reducer;
hooks.tsx
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import type { RootState, AppDispatch } from './store';
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
store.tsx
import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit';
import gridRowReducer from './gridRowSlice';
export const store = configureStore({
reducer: {
stocksList: gridRowReducer,
},
});
export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof store.getState>;
export type AppThunk<ReturnType = void> = ThunkAction<ReturnType, RootState, unknown, Action<string>>;
In this component I have used dispatch to send some data from the table.
import { move } from '../../util/redux/gridRowSlice';
import { useAppDispatch } from '../../util/redux/hooks';
const table = () => {
useEffect(() => {
dispatch(move(tableData));
});
return (
<div>
//code for table
</div>
);
};
export default table;
And in other component I want to access the value of updated state and I tried different approach but non is working for me.
This is what I have tried.
import { store } from '../util/redux/store';
import { useAppSelector } from '../util/redux/hooks';
const receiveData = () => {
const data = useAppSelector(movedData);
const view = store.getState();
const view2 = useAppSelector((state) => state.stocksList.activeRow);
useEffect(() => {
console.log(store.getState(), 'data1'); //returning blank object
console.log(data, 'data2'); //returning blank object
console.log(view, 'data3'); //returning blank object
console.log(view2, 'data4'); //returning blank object
});
return (
<div>
//code
</div>
);
};
export default receiveData;
Note:- I want to get the value of state in another tab also when its opened while the application is running.
答案1
得分: 1
Redux Store 中存储的数据通常是一个全局 JavaScript 对象。
除非我们将变量持久化存储在某个地方,否则无法从一个标签中访问在另一个标签中声明的变量。例如,将其存储在 LocalStorage 中并通过 storage event 进行同步。
一个简单的解决方案是使用 redux-state-sync
来在多个标签之间同步数据。
如果您的应用程序需要在重新加载后保留数据,您可以使用 redux-persist
进行持久化存储。
参考链接:https://github.com/aohua/redux-state-sync
英文:
The data stored in the Redux Store is typically a Global JS Object.
You cannot access a variable that is declared in a tab from another, until we persist it somewhere. Eg. Storing it in LocalStorage and syncing it via storage event
Simple solution should be using redux-state-sync
to sync the data across multiple tabs.
If your application requires the data to be there even after reloading, you can persist it using redux-persist
Reference: https://github.com/aohua/redux-state-sync
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论