I’d like to ask my senior brothers for advice.
I created a component Component and wrote a jump function similar to this.context.router.push("/user/list") inside an ajax success callback. At the same time, Component.contextTypes={ router: React.PropTypes.object.isRequired } is written outside the component. The ajax request was also successful, but the page did not jump, which is a bit doubtful. . .
The code structure is similar:
class Component extends React.Component{
...
success: function(data) {
alert(data);
this.context.router.push(...)
}
}
Component.contextTypes={
router: React.PropTypes.object.isRequired
}
Can’t get this?. Try using success()->()
Here I will write about the pitfalls I encountered when searching for answers on the Internet. At the same time, it is also to inform novices who encounter the same or similar problems later. Please also ask the relevant post managers not to delete:
In Component.contextTypes, I found it Someone wrote it inside the component this way:
However, when I do this, I always get a problem here, that is, the error "Cann't read the property 'push' is not defined" is reported. It’s not obvious why, let’s write it down first
"Cann't read the property 'push' is not defined"
This error ensures that contextTypes is written
and the constructor does not lose the context when calling super