Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

javascript - 反应:页面加载后第一次导航时,location.state是未定义的(React: location.state is undefined on first time navigating after page load)

I have a React component, which display a Link:(我有一个React组件,它显示一个链接:)

Magic.tsx:(Magic.tsx:) const { message } = this.props; <Link to={ { pathname: HOGWARTS, state: { message } } }> Go to Page </Link> I am passing this Magic Component as a Prop to another component, where I am iterating over it, with Dynamic message state(我正在将该Magic组件作为Prop传递给另一个组件,在其中我以动态message状态对其进行迭代) Parent.tsx(Parent.tsx) const CustomComp = this.props.Magic; const content = messageArray.map(msg => <CustomComp message={ msg } />) { content } //Render all the Links with message state This is rendering the Link correctly.(这将正确呈现链接。) But when I click on the Link and debug the HOGWARTS page, the location.state is undefined.(但是,当我单击链接并调试HOGWARTS页面时,location.state是未定义的。) If go back to previous page, and click again, the location.state is correct, having message data.(如果返回上一页,然后再次单击,则location.state正确,包含message数据。) So somehow it's not working on page load, but after clicking on second time, it works.(因此,它不能以某种方式在页面加载中工作,但是在第二次单击后,它可以工作。) Did anyone faced the same issue?(有人遇到过同样的问题吗?) Note: I've inspected the <Link /> tag using React Devtool , on sidebar it shows that, what message state is attached to this link.(注意:我已经使用React Devtool检查了<Link />标签,在侧边栏显示了该链接附加了什么message状态。)   ask by Rahul Sagore translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

'state'匹配'BrowserRouter','hash'匹配'HashRouter',因此如果您使用'HashRouter'匹配'hash','state'必须未定义


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...