material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

MenuItem onClick should have a Typecheck od function

Open CyanGlory opened this issue 9 years ago • 5 comments

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

when provide a prop with onClick in MenuItem like:

this.handleDialogControl('open', 'dialogPartManage')

a exec funtion, should throw Error

Current Behavior

when I use this to open dialog, and forget to set it as a arrow-function, like

() => this.handleDialogControl('open', 'dialogPartManage')

It will cause a crash that this page will keep loading.

Steps to Reproduce (for bugs)

  1. create a dialog
  2. open it with a MenuItem's onClick prop, with some code like:
// handle method
handleDialogControl = (method, boxName) => {
    const list = [...this.state.openList];
    
    if (method === 'close') {
      const openList = removeFromOpenList(list, boxName, this.enum);
      return this.setState({ openList });
    }

    if (method === 'open') {
      list.push(boxName);
      return this.setState({ openList: list });
    }

    return new Error('UserError: params invalid');
  }
// use with onClick
<MenuItem onClick={this.handleDialogControl('open', 'dialogAddLink')}>
  1. then open it in your browser
  2. unexpected keep loading.

Context

the Error is difficult to find,(also meet) I should provide some suggest, for this case.

Your Environment

Tech Version
Material-UI ^1.0.0-beta.16
React ^16.0.0
browser chrome at 61.0.3163.100
etc

CyanGlory avatar Oct 14 '17 05:10 CyanGlory

Regarding your code I think it's normal, on each render the function is called with a setState inside which will make another render etc ..

pradel avatar Oct 14 '17 09:10 pradel

If you set an arrow function your function will only be called when an event is fired

pradel avatar Oct 14 '17 09:10 pradel

so I sugguest a validate, It should be a function, in the PropType required, otherwise being errered for a good Tips I meet this by a miss, there could be other unlucky guys

CyanGlory avatar Oct 14 '17 10:10 CyanGlory

I can't understand at all what is being described here, but it doesn't seem to have anything to do with TypeScript...

pelotom avatar Nov 16 '17 06:11 pelotom

so I sugguest a validate, It should be a function, in the PropType required, otherwise being errered for a good Tips

@CyanGlory This is outside of the scope of Material-UI. It sounds like a React related issue. Maybe it's React how should warn when providing a undefined onClick property.

oliviertassinari avatar Dec 09 '17 15:12 oliviertassinari