简体   繁体   English

我正面临“TypeError:无法读取未定义的属性(读取'push')”的错误

[英]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.

相关问题 未捕获的类型错误:无法读取未定义的属性(读取“替换”)。 我在我的网站上遇到了这个错误 - Uncaught TypeError: Cannot read properties of undefined (reading 'replace'). I am facing this error in my site 在反应中遇到此错误“TypeError:无法读取未定义的属性(读取'find')” - facing this error in react "TypeError: Cannot read properties of undefined (reading 'find')" 错误类型错误:无法读取未定义的属性(读取“推送”) - ERROR TypeError: Cannot read properties of undefined (reading 'push') 使用 ipcRenderer 时出现错误(类型错误无法读取未定义的属性(读取“发送”)) - I am getting an error when using ipcRenderer (typeerror cannot read properties of undefined (reading 'send')) TypeError:无法在 React 中使用下拉 select 读取未定义的属性(读取“推送”) - TypeError: Cannot read properties of undefined (reading 'push') with dropdown select in React TypeError:无法在 React Js 中读取未定义的属性(读取“推送”) - TypeError: Cannot read properties of undefined (reading 'push') in React Js 反应:TypeError 无法读取未定义的属性(读取“推送”) - React: TypeError cannot read properties of undefined (reading 'push') 类型错误,无法读取 JavaScript 中未定义的属性(读取“推送”) - TypeError, Cannot read properties of undefined (reading 'push') in JavaScript TypeError:无法读取未定义的属性(读取“推送”)反应路由器 DOM - TypeError: Cannot read properties of undefined (reading 'push') React router DOM 类型错误:无法读取未定义的属性(读取“地图”)。 当我尝试通过一系列加密货币 map 时抛出错误 - TypeError: Cannot read properties of undefined (reading 'map'). Error being thrown when I am trying to map over an array of cryptocurrencies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM