[英]Using Redux Toolkit, how do I access the store from a non-react file?
What am I trying to do?我想做什么?
Using Redux Toolkit, I'm trying to access the "store" for a value, specifically "username" which I've created a "slice" for, from a non-React file called SomeFile.js
.使用 Redux Toolkit,我试图从一个名为
SomeFile.js
的非 React 文件中访问一个值的“存储”,特别是我创建了一个“切片”的“用户名”。
What is the code that currently tries to do that?当前尝试执行此操作的代码是什么?
// userMetadataSlice.js
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
username: "",
};
const userMetadataSlice = createSlice({
name: "userMetadata",
initialState,
reducers: {
updateUsername: (state, action) => {
const username = action.payload;
state.username = username;
},
},
});
export const { updateUsername } = userMetadataSlice.actions;
export default userMetadataSlice.reducer;
export const selectUsername = (state) => {
return state.userMetadata.username;
}
// SomeFile.js
import { selectUsername } from "../redux/userMetadataSlice";
import { useSelector } from "react-redux";
export const displayUsername = () => {
const username = useSelector(selectUsername);
console.log("Username:", username); // Error.
}
What do I expect the result to be?我期望结果是什么?
To be able to pull the username from the "store".为了能够从“商店”中提取用户名。
What is the actual result?实际结果是什么?
When I try to access the value via "useSelector" from the non-react file an error occurs: React Hook "useSelector" is called in function "selectUsername" which is neither a React function component or a custom React Hook function
当我尝试通过非反反应文件中的“使用者”访问该值时,发生错误:
React Hook "useSelector" is called in function "selectUsername" which is neither a React function component or a custom React Hook function
What I think the problem could be?我认为问题可能是什么?
SomeFile.js
does not have anything React related within it because it just pulls data from the store and outputs the data. SomeFile.js
中没有任何与 React 相关的内容,因为它只是从存储中提取数据并输出数据。
A solution I've tried that worked was to do this:我尝试过的一个可行的解决方案是这样做:
// userMetadataSlice.js
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
username: "",
};
const userMetadataSlice = createSlice({
name: "userMetadata",
initialState,
reducers: {
updateUsername: (state, action) => {
const username = action.payload;
state.username = username;
},
},
});
export const { updateUsername } = userMetadataSlice.actions;
export default userMetadataSlice.reducer;
export const selectUsername = (state) => {
return state.userMetadata.username;
}
// New code here!
export function SelectUsername() {
const username = useSelector(selectUsername);
return username;
}
// SomeFile.js
import { SelectUsername } from "../redux/userMetadataSlice";
import { useSelector } from "react-redux";
export const displayUsername = () => {
console.log("Username:", SelectUsername); // No errors, shows correct output.
}
The solutions I'm looking for is this:我正在寻找的解决方案是这样的:
From your created store
object you'll have a getState
method to invoke.从您创建的
store
object 中,您将有一个getState
方法可以调用。
getState()
Returns the current state tree of your application.
返回应用程序的当前 state 树。 It is equal to the last value returned by the store's reducer.
它等于 store 的 reducer 返回的最后一个值。
Returns
退货
(any): The current state tree of your application.
(任意):您的应用程序的当前 state 树。
You can export your created store object for import into non-React JS files and they can invoke the getStore
method.您可以导出您创建的商店 object 以导入非 React JS 文件,它们可以调用
getStore
方法。
import store from '../path/to/store';
...
const state = store.getState();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.