简体   繁体   中英

How to redefine a Class Component method in React?

I want to redefine a function/method inside a Class Component because I want to reuse it.

I tried

ClassComponent.prototype.submitForm = async () => {
...
}

but no luck.

In Jest, it is possible to do

jest.spyOn(class.prototype, 'method').mockImplementation

to change the implementation of the method. But I want to do it in React. Is there a way to do this? I need to change that method so I can fully reuse my Component. Thanks

I don't have your code to have a better context, but from what I see - you're going in a wrong direction. If you wish to overwrite the behavior of an inner-function inside the component, just overwrite it when a certain condition applies. You can check this condition in an event/hook or on init.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM