我如何从 Appjs 中的商店获取状态?
How can i get State from my store in Appjs?
我想在我的 App.js 中查看状态
我是这样导入的
import configureStore from './src/configureStore'
let store = configureStore()
所以我正在注射 App.js
<Provider store={store} theme={theme}>
{console.log('11111',store)}
<NavigationContainer>
只有在控制台我才能看到这个:
{dispatch: ƒ, subscribe: ƒ, getState: ƒ, replaceReducer: ƒ, @@observable: ƒ}
如何查看状态?
顺便说一句:configureStore 是:
import {createStore, applyMiddleware} from 'redux';
import Reducers from './reducers'
import thunk from 'redux-thunk'
export default function configureStore () {
let store = createStore(Reducers, applyMiddleware(thunk))
return store
}
您只需调用 store.getState() 方法即可获取应用程序的当前状态树。它等于商店的减速器返回的最后一个值。
<Provider store={store} theme={theme}>
{console.log('11111',store.getState())}
<NavigationContainer>
为此,您需要来自“react-redux”
的选择器挂钩
...
import {useSelector} from "react-redux";
...
...
const {yourStateKey} = useSelector((state) => state);
...
这部分可以根据您拥有多少家商店而有所不同
(state) => state
要在商店中设置状态值,请使用“react-redux”
中的“useDispatch”挂钩
...
import {useDispatch} from "react-redux";
...
const dispatch = useDispatch();
...
dispatch(reduxActions.yourAction(yourValue))
我想在我的 App.js 中查看状态 我是这样导入的
import configureStore from './src/configureStore'
let store = configureStore()
所以我正在注射 App.js
<Provider store={store} theme={theme}>
{console.log('11111',store)}
<NavigationContainer>
只有在控制台我才能看到这个: {dispatch: ƒ, subscribe: ƒ, getState: ƒ, replaceReducer: ƒ, @@observable: ƒ}
如何查看状态?
顺便说一句:configureStore 是:
import {createStore, applyMiddleware} from 'redux';
import Reducers from './reducers'
import thunk from 'redux-thunk'
export default function configureStore () {
let store = createStore(Reducers, applyMiddleware(thunk))
return store
}
您只需调用 store.getState() 方法即可获取应用程序的当前状态树。它等于商店的减速器返回的最后一个值。
<Provider store={store} theme={theme}>
{console.log('11111',store.getState())}
<NavigationContainer>
为此,您需要来自“react-redux”
的选择器挂钩...
import {useSelector} from "react-redux";
...
...
const {yourStateKey} = useSelector((state) => state);
...
这部分可以根据您拥有多少家商店而有所不同
(state) => state
要在商店中设置状态值,请使用“react-redux”
中的“useDispatch”挂钩...
import {useDispatch} from "react-redux";
...
const dispatch = useDispatch();
...
dispatch(reduxActions.yourAction(yourValue))