получить событие нажатия кнопки без холста - PullRequest
1 голос
/ 09 мая 2019

Как я могу иметь 2 разных события нажатия кнопки и ListItem?
Я в основном заинтересован в событии нажатия кнопки без события ListItem.

CodeSandobox DEMO

import React from "react";
import ReactDOM from "react-dom";
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItemText";

function App() {
  const handleButtonClick = () => {
    console.log("button click");
  };
  const handleListItemClick = () => {
    console.log("list click");
  };
  return (
    <ListItem button onClick={() => handleListItemClick()}>
      <button onClick={() => handleButtonClick()}>
        get only the button click
      </button>
      <ListItemText primary="without the ListItem click event..." />
    </ListItem>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

1 Ответ

1 голос
/ 09 мая 2019

Пожалуйста, отметьте это

import React from "react";
import ReactDOM from "react-dom";
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItemText";

function App() {
  const handleButtonClick = e => {
    e.stopPropagation();
    console.log("button click");
  };
  const handleListItemClick = () => {
    console.log("list click");
  };
  return (
    <ListItem button onClick={() => handleListItemClick()}>
      <button onClick={e => handleButtonClick(e)}>
        get only the button click
      </button>
      <ListItemText primary="without the ListItem click event..." />
    </ListItem>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...