Javascript Redux希望侦听器能够正常工作
你好,我正在使用redux,我正在解决这个问题。 我正在从redux向内置的subscribe方法添加一个侦听器函数Javascript Redux希望侦听器能够正常工作,javascript,redux,Javascript,Redux,你好,我正在使用redux,我正在解决这个问题。 我正在从redux向内置的subscribe方法添加一个侦听器函数 store.subscribe(historyManager(store.getState())); 我将状态传递给这个侦听器 侦听器本身如下所示: const historyManager = function (state) { // if(['channel', 'account', 'tab', 'main', 'ui.sub'].indexOf(changed
store.subscribe(historyManager(store.getState()));
我将状态传递给这个侦听器
侦听器本身如下所示:
const historyManager = function (state) {
// if(['channel', 'account', 'tab', 'main', 'ui.sub'].indexOf(changedVal)) return pushState();
// 'ui.piet' // wek
// 'ui.piet.klaas.jan' // niet
// if(['set', 'in', 'url'].indexOf(changedVal)) return replaceState();
// doe geen zak
console.log(state)
};
忽略这些评论,因为现在我只是担心记录我从订阅服务器传递的状态
在我的控制台中,我得到一个错误:
redux.js:138 Uncaught Error: Expected the listener to be a function.
可能是因为我的函数没有返回语句吗 调用store.subscribe时直接调用historyManager。这样,您将未定义的消息传递给store.subscribe,而不是回调
您要做的是将函数传递给subscribe方法
store.subscribe(historyManager)
然后redux本身将调用historyManager。您可以通过从historyManager调用store.getState来获取当前状态
只要去商店。subscribehistoryManager@Federkun那么,如何将状态传递给侦听器函数?
const store = ...
const historyManager = function () {
const state = store.getState()
};
store.subscribe(historyManager);