简体   繁体   English

React.js onShow 事件处理程序

[英]React.js onShow event handler

我的页面上有一个选项卡式菜单,基本上是一堆 div,根据单击的选项卡显示\/隐藏。一旦出现,我希望能够在其中一个 div 中重新获取 React 组件中的数据。

componentDidMount<\/code>在一开始就触发,所以它不是一个选项。最好有一个类似于onClick<\/code>的 HTML 事件onShow<\/code> ,但似乎不存在这样的事件。

有什么建议?

"

I have a Tabs component but in my case, all tabs content are mounted but not displayed.我有一个选项卡组件,但在我的情况下,所有选项卡内容都已安装但未显示。 When I click on a tab, I just change the visibility with css当我单击一个选项卡时,我只是用 css 更改可见性

But to refetch data, you should make a component for the containers that fetch the data in his componentDidMount so, in the top level if you switch which one renders, the componentDidMount will trigger.但是,重新获取数据,你应该为他取数据的容器组件componentDidMount如果切换哪一个渲染所以,在顶级水平, componentDidMount将触发。

Put some code if you want some help with your case如果您需要有关案例的帮助,请输入一些代码

Per the docs , componentDidUpdate is probably what you're looking for.根据文档componentDidUpdate可能是您正在寻找的。 It's called after props and/or state has changed and the component has re-rendered (not called for the initial render).props和/或state发生变化并且组件重新渲染(初始渲染时不调用)之后调用它。

That said, it's probably simpler to just handle the data fetching in the onClick handler itself or better yet, fetch the data once in the parent component and pass down the relevant slice of data to the tabs and then show / hide as appropriate.也就是说,仅在onClick处理程序本身中处理数据获取可能更简单,或者更好的是,在父组件中获取数据一次并将相关的数据片段传递给选项卡,然后根据需要显示/隐藏。

嘿,您必须将选项卡作为组件分离出来,并根据选项卡更改来渲染这些组件,让我在这里举一个例子。

假设我在该页面中有一个名为 MainPage 的页面,我有选项卡控件,其中包含三个选项卡第一、第二和第三。

首先,您必须创建一个三个组件,例如

var First=React.createClass({ // First Tab Content goes here });
var Second=React.createClass({// Second Tab Content goes here});
var Second=React.createClass({// Third Tab Content goes here});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React.js-通过事件处理程序更改所有者组件的状态 - React.js - Changing state of owner component by an event handler 使用事件处理程序更新原始状态react.js - Using event handler to update the original state react.js react.js中的事件组合 - Event composition in react.js 使用react.js JSX页面时onClick按钮事件处理程序不起作用 - onClick button event handler not working when using react.js JSX pages React.js 中的点击处理程序在事件发生之前执行。 我怎么能阻止呢? - The on click handler in React.js executes before the event happens. How can I stop that? 如何在 react.js 中使用 onclick 事件处理程序调用 class 组件 - How to call a class component using onclick event handler in react.js react.js:如何将传入的事件处理程序的this绑定到子组件? - react.js: How to bind passed-in event handler's this to child component? React.js:在组件中没有 bind() 的情况下将参数传递给事件处理程序的最有效方法 - React.js: the most efficient way to pass a parameter to an event handler without bind() in a component 警告:React.js web 应用程序中的未知事件处理程序属性`onClick` - Warning: Unknown event handler property `onClick` in React.js web app React.js:为什么没有componentDidRender事件? - React.js: Why is there no componentDidRender event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM