[英]Can I change my Redux state from outside a function component?
我将我的 api 访问令牌存储在 Redux state 中。 当我的访问令牌过期时,我的 api 将刷新的令牌作为 header 发回。 我要做的是设置一个 axios 拦截器,如果发回刷新的令牌,它将更改我的 state。
const myAxios = axios.create()
myAxios.interceptors.response.use(
(response) => {
if (response.headers['x-access-token']) {
// Is there a way to dispatch something here to change state
}
return response
},
async function (error) {
return Promise.reject(error)
}
)
export default myAxios
所以有了这个我可以得到我的令牌我只是不知道如何从这里更改 state 因为 useDispatch 不能从功能组件外部工作
首先,导出您的商店:
export const store = configureStore({
...
});
然后,将其导入您的文件并使用它:
import { store } from './store';
...
store.dispatch() // got dispatch
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.