[英]I am facing an error of "TypeError: Cannot read properties of undefined (reading 'push')"
I have been facing this issue while using history.push('/pathname') in app.js component.我在 app.js 组件中使用 history.push('/pathname') 时遇到了这个问题。 my requirement is that I have to use this history.push inside an onclick handler defined in app.js component which is then sent to button component in via props.我的要求是我必须在 app.js 组件中定义的 onclick 处理程序中使用这个 history.push,然后通过 props 将其发送到按钮组件。 I am not able to figure out the solution of this error.我无法找出此错误的解决方案。 (The button component is AppointmentNextBtn). (按钮组件是 AppointmentNextBtn)。 Please guide me.请指导我。
import { useHistory } from "react-router-dom";
function App(){
const history = useHistory();
const onClickNextDateHandler = () => {
history.push("/selectlocation/appointment/selectdate");
console.log("date button runs");
setNextBtnState("time");
}; };
return(
<Route exact path="/selectlocation/appointment">
<Toappointmentcontainer
postAppointmentRequest={postRequestAppointment}
/>
<div className="btnwidget">
<AppointmentNextBtn
onClickDateHandler={onClickNextDateHandler}
displayState={nextBtnState}
text="Next"
button="button"
/>
</div>
</Route>
) )
}
You cannot use history in the same file you declare your route.您不能在声明路线的同一文件中使用历史记录。 History objects will always be unavailable in this case.在这种情况下,历史对象将始终不可用。 Best way is to do directly it in appointmentNextBtn.最好的方法是直接在约会NextBtn中进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.