简体   繁体   English

如何在 onClick 事件处理程序上渲染组件

[英]how to render component on onClick event handler

I am trying to open a Popper element when a user clicks a button (using onClick) and render the Popper component, since i'm using this function call in a array list of items.当用户单击按钮(使用 onClick)并呈现 Popper 组件时,我试图打开一个 Popper 元素,因为我在项目数组列表中使用此 function 调用。 I want to execute the element after the button is triggered.我想在按钮被触发后执行元素。

onClick Handler onClick 处理器

const onAddToPlaylistClick = (evt, ytid) => {
  evt.stopPropagation();
  evt.preventDefault();

  console.log('here');
  const element = evt.currentTarget;
  console.log(element);   
  return (
    <ShowAddToPlaylistTooltip
      ytid={ytid}
      element={element}
      playlistState={playlistState}
      userState={userState}
      key={ToolTipKeys.AddToPlaylistToolTip}
    />
  );
};

Button Component按钮组件

const onAddToPlaylistClick = (evt) => {
  props.onAddToPlaylistClick(evt, props.ytid);
};

<IconButton className={classes.iconButton} onClick={onAddToPlaylistClick}>
  <PlaylistAddIcon className={classes.icon} />
</IconButton>

Popper Element波普尔元素

const ShowAddToPlaylist = (props) => {
  const { ytid, userState, playlistState, element } = props;

  console.log('ShowAddToPlaylist...');

  const open = Boolean(element);

  const uid = userState.profile.uid;
  const userPlaylists = playlistState.playlistsByUser[uid] || [];

  const playlistList = userPlaylists.map((pid) => ({
    name: (playlistState.playlists[pid] ?? {}).name,
    pid: pid,
  }));

  const hasPlaylists = playlistList.length > 0;

  return (
    <Tooltip
      id={props.id}
      open={props.open}
      title="Add to playlist"
      anchorRef={element}
    >
      <AddPlaylistToolTip
        ytid={ytid}
        playlistList={playlistList}
      />
    </Tooltip>
  );
};

but onAddToPlaylistClick doesn't trigger React Element, what is it that i'm doing wrong?但是 onAddToPlaylistClick 不会触发 React Element,我做错了什么?

You can't render JSX from onAddToPlaylistClick function, but you can implement some logic that will render popover (or if you use some Router so redirect...)你不能从onAddToPlaylistClick function 渲染 JSX,但是你可以实现一些逻辑来渲染 popover(或者如果你使用一些路由器,所以重定向......)

const onAddToPlaylistClick = (evt, ytid) => {
  evt.stopPropagation();
  evt.preventDefault();
  console.log('here');
  const element = evt.currentTarget;
  console.log(element);  
  this.setState({ element }) 
};

/// inside render (or `return` for function component...)

{ this.state.element && <ShowAddToPlaylistTooltip
      ytid={ytid}
      element={this.state.element}
      playlistState={playlistState}
      userState={userState}
      key={ToolTipKeys.AddToPlaylistToolTip}
    /> 
}

if element state initially is null , the popover gonna render only after click.如果element state 最初是null ,则仅在单击后才会呈现弹出窗口。 for closing it use setState({ element: null })关闭它使用setState({ element: null })

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

相关问题 如何在 reactjs 中使用 onclick 事件渲染子组件? - How to render child component with onclick event in reactjs? 如何在 onClick 事件反应 js 上从子级渲染其他组件? - How to render other component from children on onClick event react js? React组件无法更新onClick事件处理程序 - React component fails to update onClick event handler 在onclick事件上提供ReactJS-渲染模式组件 - ReactJS- render modal component on onclick event 如何处理父组件中子组件的onClick - How to handler onClick on child component in parent component 如何在onClick上呈现不同的组件? (组件应由最新点击的处理程序替换。) - How do I render different components onClick? (Component should be replaced by the newest clicked handler.) 如何使用 Card.js 组件上的 onClick 事件在 React 中渲染 Article.js 组件? - How to use onClick event on Card.js component to render Article.js Component in React? 如何使用另一个组件中的按钮渲染组件 onClick? - How to render a component onClick with a button in another component? 从Reactjs中的onClick事件渲染组件中的组件 - Render a component within a component from onClick event in Reactjs 如何在 react.js 中使用 onclick 事件处理程序调用 class 组件 - How to call a class component using onclick event handler in react.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM