在redux-thunk设置中应用跟踪选项可以通过使用中间件来实现。以下是一些代码示例,帮助你实现这个目标:
import thunk from 'redux-thunk';
import { applyMiddleware, createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly';
// 定义一个跟踪中间件
const traceMiddleware = () => next => action => {
console.log(`Action: ${action.type}`);
return next(action);
};
// 创建一个 Redux store
const store = createStore(
rootReducer,
composeWithDevTools(
applyMiddleware(thunk, traceMiddleware)
)
);
在这个例子中,我们定义了一个名为'traceMiddleware”的中间件,它将打印出每个传递给 store.dispatch() 的 action 的类型。我们在创建Redux store时,将中间件应用到了thunk中,并将其传递给applyMiddleware()函数,以便在store.dispatch()期间执行它。
可以在该中间件中添加任何你希望跟踪的内容,以便更好地了解Redux在你的应用中的运行情况。