简体   繁体   English

如何在 ReactJS 中单击按钮

[英]How can I focus a button on click in ReactJS

I have the following problem, in my web site build with nextJS and ReactJS with typescript I have products that are loaded when a button is clicked, when I click the button the items appeared and the button is scrolled down, which is the asked behavior, but when I scroll to the bottom of the page and I try to click the button the scroll remains on the same position and the items are loaded but cannot be seen, my logic is to use onFocus on the current button and when I click it to change the scroll to him, that will solve the problem when the user has scrolled down to the bottom of the page, that way it will not remain on the bottom but rather it will automatically scroll up to the button and will see the new items loaded.我有以下问题,在我的 web 网站构建中,使用 nextJS 和 ReactJS 使用 typescript 我有产品被加载,当单击按钮时出现询问行为,当我单击按钮并向下滚动按钮时,该按钮是但是当我滚动到页面底部并尝试单击按钮时,滚动保持在相同的 position 并且项目已加载但看不到,我的逻辑是在当前按钮上使用 onFocus 并且当我单击它时将滚动更改为他,这将解决用户向下滚动到页面底部时的问题,这样它就不会留在底部,而是会自动向上滚动到按钮并会看到加载的新项目.

The problem is that the logic to load the products are in a different component in which I am reusing the current button and right prop I am sending function to the onClick on the button.问题是加载产品的逻辑位于不同的组件中,我在其中重用当前按钮和正确的道具,我将 function 发送到按钮上的 onClick。 My question is how can I use onFocus.我的问题是如何使用 onFocus。 Does it has to be in the child component inside the function or in the button component.它必须在 function 内的子组件中还是在按钮组件中。 I tried to make it work on the Button component, but it doesn't work.我试图让它在 Button 组件上工作,但它不起作用。 So I am stuck for the last 4 hours and I really need a push.所以我在过去的 4 个小时里被困住了,我真的需要推动。 I would be glad if you could shine some enlargement如果你能放大一点我会很高兴

Here I will enter the function in the parent component for the onClick prop:这里我将在 onClick prop 的父组件中输入 function:

const handleLoadMoreProducts = () => {
    if (!isSearchPage) {
      const mappedBreadcrumbs: string[] = categoryData.breadcrumbs.map(
        (crumb: BreadCrumItem) => crumb.name
      );
      gtmBreadcrumbTrack(mappedBreadcrumbs);
    }

 <LoadMoreProducts handleLoadMoreProducts={handleLoadMoreProducts} />

And here is the component that uses the Button:这是使用 Button 的组件:

interface LoadMoreProductsProps {
  handleLoadMoreProducts?: (MouseEvent: React.MouseEvent<HTMLButtonElement>) => void;
  Focus?: (MouseEvent: React.MouseEvent<HTMLButtonElement>) => void;
}

const LoadMoreProducts: FC<LoadMoreProductsProps> = ({ handleLoadMoreProducts }) => (
  <div className="LoadMoreProducts">
    <Button type="button" importance="ghost" onClick={handleLoadMoreProducts}>
      Load more products
    </Button>
  </div>
);

I think what you want to do is to forward the ref of the element you are trying to focus in the Button component using React.forwardRef and combine it with the useImperativeHandle hook in order to gain the ability to trigger the focus with the ref outside of the Button component.我认为您想要做的是使用React.forwardRef转发您尝试在 Button 组件中关注的元素的引用,并将其与useImperativeHandle钩子结合起来,以获得使用外部引用触发焦点的能力按钮组件。

You could create a ref for the element you are trying to focus and call the focus() function for the ref on click.您可以为您尝试聚焦的元素创建一个引用,并调用focus() function 以获得单击时的引用。

More information regarding forwarding refs and the useImperativeHandle hook.有关转发 refsuseImperativeHandle钩子的更多信息。

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

相关问题 在Angular 2中,如何在单击按钮时将注意力集中在div的底部 - In Angular 2, how can I focus to the bottom of a div on button click 单击Primefaces上的重置按钮时,如何将焦点设置在inputText上 - how can I set the focus on inputText when I click reset button on Primefaces 单击按钮时如何将焦点保持在所选文本上? - How can I keep the focus to my selected text when I click a button? 当基于具有焦点的文本框按下Enter键时,如何触发Button Click事件? - How can I fire a Button Click event when Enter Key is pressed based on what textbox has focus? 我如何在上一个/后退上保存状态(数据)按钮在ReactJS中单击 - How i can save state ( data ) on Previous/Back" button click in ReactJS 如何通过单击 Reactjs 中的按钮将 FormControl 从禁用切换为启用 - How can I toggle a FormControl from disabled to enabled with a button click in Reactjs 当单击 reactjs 的其他文件中的按钮时,我如何使进度条取得进展 - How i can make progress bar make progress when click a button in other file at reactjs 如何在 ReactJS 中使用 css 调整按钮的大小 - How can I resize a button with css in ReactJS 在按钮上单击如何更改焦点 - On button click how to change focus 如何在 ReactJS 中重定向按钮点击 - How to redirect on button click in ReactJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM