我是 React 和 Redux 的新手。我有一个简单的应用程序,您可以在其中将输入文本添加到无序列表中。我有防止显示特定单词的中间件。我不明白实现中间件的操作顺序。据我了解,当我触发调度时,会发生以下情况:
从表单提交事件触发调度
// title is an object with a string
function handleSubmit(event) {
props.addArticle(title);
}
通过中间件功能:
// store.js
const store = createStore(
rootReducer, // this is defined elsewhere and effectively handles actions
applyMiddleware(forbiddenWordsMiddleware)
);
// middleware.js
import { ADD_ARTICLE } from "../constants/constants.js";
const forbiddenWords = ["spam", "money"];
export function forbiddenWordsMiddleware({ dispatch }) {
return function (next) {
return function (action) {
if (action.type === ADD_ARTICLE) {
const foundWord = forbiddenWords.filter((word) =>
action.payload.title.includes(word)
);
if (foundWord.length) {
return dispatch({ type: "FOUND_BAD_WORD" });
}
}
return next(action);
};
};
}
上述函数实际上如何与 createStore 和 applyMiddleware 一起工作?让我特别困惑的是这next(action)条线。下一步和行动从何而来?我无法想象从表单提交到检查禁用词的线性执行。
www说
慕容森
SMILET
随时随地看视频慕课网APP
相关分类