我正在使用React-Simple-Chatbot库中的ChatBot组件。该组件具有一个称为triggerNextStep
的功能。我想装饰the triggerNextStep function。
扩展并不能完全起作用,因为如果我打电话给super
会收到一个错误消息。
class WrappedChatBot extends ChatBot {
triggerNextStep = (...args) => {
console.log("hello");
super.triggerNextStep(...args)
}
}
阅读我发现,做事的反应方式是改为使用高阶组件。为此,我尝试了以下方法:
var wrapChatBot = (componentClass) => {
return class extends componentClass {
triggerNextStep(...args) {
console.log("hello you")
super.triggerNextStep(...args)
}
}
}
const WrappedChatBot = wrapChatBot(ChatBot)
使用上面的代码,该应用程序可以正常运行,但是我看不到“你好”被登录到控制台。似乎只是忽略了HoC。顺序重要吗?我的语法中缺少什么吗?