简体   繁体   English

Material-UI MenuItem自动将参数发送给onClick函数吗?

[英]Material-UI MenuItem automatically sends argument to onClick function?

I have a search.js file and a search-date.js file. 我有一个search.js文件和一个search-date.js文件。 In the search.js file, I render a SearchDate container. 在search.js文件中,我呈现了SearchDate容器。 What I don't understand is the behaviour of the MenuItem component when it is clicked. 我不了解的是单击MenuItem组件时的行为。

As you can see, the function onDayChange is passed down from Search to SearchDate . 如您所见, onDayChange函数从Search传递到SearchDate This function is then passed to MenuItem on the onClick property. 然后将此函数传递给onClick属性上的MenuItem onDayChange in Search needs a date argument. Search onDayChange需要一个date参数。

Right now the alert call I've made outputs: object . 现在,我发出了alert :output: object Where does this object come from? 这个物体从哪里来? I can't see anywhere in my code that it's being sent by me. 我在代码中的任何地方都看不到它是由我发送的。 And I'm not sure where to look in the Material-UI docs. 而且我不确定在Material-UI文档中可以找到哪里。

search.js: search.js:

import SearchDate from '../components/search-date';
import { modelInstance } from '../model/model';    

class Search extends Component {
  constructor(props){
    super(props)
    this.state = {
      data: null,
      searchSuggestion: 'Search for tweets here',
      anchorEl: null,
      date: 'Today',
      page: 0,
      placeName: 'the World'
    }

  componentDidMount() {
    modelInstance.addObserver(this);
  }

  handleClick = event => {
    this.setState({ anchorEl: event.currentTarget });
  };

  onDayChange = date => {
    alert(typeof date);
    this.setState({date: date})
    this.setState({ anchorEl: null });
  };



  render(){
    return(
        <div className='search'>
          <Row id='searchInput'>
            <SearchInput handleInput={this.handleInput.bind(this)} searchInput={this.state.searchInput} searchSuggestion={this.state.searchSuggestion} page={1}/>
          </Row>
          <Row>
            <SearchNav page={this.state.page}/>
          </Row>
          <Row id='date-location'>
            <Col xs={2} sm={2} md={2} className='text'>
              <p>FROM</p>
            </Col>
            <Col xs={4} sm={4} md={4} className='date'>
              <SearchDate date={this.state.date} anchorEl={this.state.anchorEl} click={this.handleClick} dayChange={this.onDayChange}/>
            </Col>
            <Col xs={2} sm={2} md={2} className='text'>
              <p>IN</p>
            </Col>
            <Col xs={4} sm={4} md={4} className='location'>
              <SearchLocation placeName = {this.state.placeName} handleLocation={this.handleLocation.bind(this)}/>
            </Col>

          </Row>
        </div>
    )
  }
}

export default Search;

search-date.js: 搜索date.js:

const SearchDate = ({date, anchorEl, click, dayChange}) => {   
    return(
    <React.Fragment>
      <Button
        // variant="raised"
        aria-owns={anchorEl ? 'simple-menu' : null}
        aria-haspopup="true"
        onClick={click}
        margin={10}
      >
        {date}
      </Button>
      <Menu
        id="simple-menu"
        anchorEl={anchorEl}
        open={Boolean(anchorEl)}
        onClose={dayChange}
      >
        {/* {daysList} */}

        <MenuItem onClick={dayChange}>Yesterday</MenuItem>
        <MenuItem onClick={dayChange}>2 Days past</MenuItem>
        <MenuItem onClick={dayChange}>3 Days past</MenuItem>
        <MenuItem onClick={dayChange}>4 Days past</MenuItem>
        <MenuItem onClick={dayChange}>5 Days past</MenuItem>
        <MenuItem onClick={dayChange}>6 Days past</MenuItem>
        <MenuItem onClick={dayChange}>7 Days past</MenuItem>
      </Menu>
    </React.Fragment>
  );
}

export default withStyles(styles)(SearchDate);

Material-UI passes the DOM event as an argument on the onClick. Material-UI将DOM事件作为onClick的参数传递。

onDayChange = (date) => (event) => { ...your code }

<MenuItem onClick={onDayChange('2 days past')}>2 Days past</MenuItem>

You can pass whatever you want in the event handler. 您可以在事件处理程序中传递任何内容。 The outer function will get called at the time of rendering. 渲染时将调用外部函数。 The inner function, which has your handler, will get at the time of menu item click. 带有您的处理程序的内部函数将在单击菜单项时获得。 So your date parameter might get stale if the page doesn't refresh over night, for instance. 因此,例如,如果页面没有整夜刷新,则date参数可能会过时。 Personally, I'd pass the # of days as the argument, then get the current date in the handler and do the offset there. 就个人而言,我将传递天数作为参数,然后在处理程序中获取当前日期并在那里进行偏移。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM