For some reason in my react app, the following code triggers a compilation error in Webpack using the typescript loader (ts-loader
):
handleEdit(): void {
window.location.href = `/edit/${this.props.entry.id}`;
}
TS2540: Cannot assign to ‘href’ because it is a read-only property.
I suspect it’s some kind of odd artifact of my environment. I have this environment:
"react": "^17.0.1"
webpack: 5.61.0
webpack-cli: 4.9.1
webpack-dev-server 4.11.1
"ts-loader": "^9.2.6",
"typescript": "^4.4.4",
2
Answers
I never figured out the answer. My workaround was using
react-dom-router
and changing all instances ofwindow.location.href = ...
toreload(...)
, which solved the problem.use
window.location.assign
. from here