答案 0 :(得分:2)
需要从商店致电getState
。这是一个示例:
store.js
import { createStore } from 'redux'
function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
let store = createStore(counter)
export { store }
normal-javascript-file.js
import { store } from './store.js'
// you can call getState() to get current store state.
console.log(store.getState())
如果您的普通javascript文件未与react-app捆绑在一起,则需要将商店实例分配给window对象,以使其在世界各地都可使用:
store.js
import { createStore } from 'redux'
function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
let store = createStore(counter)
window.appStore = store
export default store
normal-javascript-file.js
var store = window.appStore
console.log(store.getState())