Reactjs 通过重定向传递状态
我正在尝试使用重定向传递状态,但收到一个错误,未定义“事件”。我不确定为什么会这样 组成部分1:Reactjs 通过重定向传递状态,reactjs,redirect,Reactjs,Redirect,我正在尝试使用重定向传递状态,但收到一个错误,未定义“事件”。我不确定为什么会这样 组成部分1: //cal_events is an array of objects <Redirect to={{ pathname: '/component2', state: { events: this.state.cal_events} }} /> 我收到错误,无法读取未定义的属性“事件”请尝试在第二个组件中使用withRouter Hoc 如果将第二个组件与withRouter HOC
//cal_events is an array of objects
<Redirect to={{
pathname: '/component2', state: { events: this.state.cal_events}
}} />
我收到错误,无法读取未定义的属性“事件”请尝试在第二个组件中使用withRouter Hoc 如果将第二个组件与withRouter HOC绑定,则可以访问该组件中的位置道具 例如:
import { withRouter } from "react-router";
class MyComponent extends Component {
...
}
withRouter(MyComponent)
谢谢,你能提供一个例子吗?@user10696040,更新了答案,请看一看,如果这解决了你的问题,请将答案标记为已接受,thanks@user10696040,请您创建一个包含问题演示的沙盒,以便我可以查看它。上的试用演示:应该有state.events,但您可以尝试在呈现函数中使用console.log,这可能是因为您在重定向之前安装了此组件。
import { withRouter } from "react-router";
class MyComponent extends Component {
...
}
withRouter(MyComponent)