【问题标题】:NextJS: How to update the state if the Link pointing at the same pageNextJS:如果链接指向同一页面,如何更新状态
【发布时间】:2019-01-29 00:08:00
【问题描述】:

我的代码在这里https://codesandbox.io/s/oo1lyw0mq9

请尝试转到查找页面。有3个链接a,b,c。单击每个链接后,我希望看到word 分别更新到链接。我知道我的代码是错误的,但正确的修复方法是什么?

【问题讨论】:

    标签: reactjs next.js


    【解决方案1】:

    只需使用:

    {this.props.word}
    

    从 props 设置状态是一种反模式:

    https://medium.com/@justintulk/react-anti-patterns-props-in-initial-state-28687846cc2e

    您的 getInitialProps 返回您已经需要的道具。

    【讨论】:

    • 感谢您的回答。我知道​​使用 this.props.word 会起作用,但是由于逻辑复杂,我想使用状态,以便我可以通过更改状态来更新 UI
    • A 将您带到另一个页面,因此将从查询中读取道具。如果您没有通过链接导航,则可以根据这些点击将它们更改为仅按钮和切换状态。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 2013-05-19
    • 2021-05-06
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    相关资源
    最近更新 更多