[英]Reactjs - Why doesn't my MaterialUI Datatable refresh?
So currently I'm trying to render a DataTable
component. 所以目前我正在尝试呈现一个
DataTable
组件。 If I click on a button Users
, it should render a table of my Users
. 如果我单击“
Users
按钮,它应该呈现“ Users
表。 When I click on the button Devices
, it should render a table of my Devices
. 当我单击
Devices
按钮时,它应该呈现我的Devices
表。 However, for some reason, it will only render the datatable of whichever one I click first. 但是,由于某种原因,它只会呈现我首先单击的那个数据表。
Eg if I click Devices
, it will render my Devices
table. 例如,如果我单击
Devices
,它将呈现我的Devices
表。 Now, when I try to navigate to my Users
by clicking on the Users
button, it'll still display my Devices
datatable. 现在,当我尝试导航到我的
Users
通过点击Users
按钮,它仍然显示我的Devices
数据表。
I maintain a state inside DataTable
and I'm updating the state with props that I pass into it. 我在
DataTable
维护一个状态,并使用传递给它的道具更新该状态。 Is there a design flaw that I made that causes my DataTable
to not render the correct data? 我是否存在设计缺陷,导致我的
DataTable
无法呈现正确的数据?
Thanks for your help! 谢谢你的帮助!
EDIT: Upon some debugging, it seems the state isn't being updated when I click on the Users
button 编辑:经过一些调试,当我单击“
Users
按钮时,似乎状态没有更新
Reading this React: Why component's constructor is called only once? 阅读此React:为什么组件的构造函数仅被调用一次? solved my problem.
解决了我的问题。
TLDR: Rendering the same component will not refresh the component. TLDR:呈现相同的组件不会刷新该组件。 To overcome this, I needed to introduce a
key
prop. 为了克服这个问题,我需要介绍一个
key
道具。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.