简体   繁体   中英

React pass props to onChange

I was wondering . Is it possible to use onChange event handler , and pass props to the method as parameters ?

For example :

    <TablePagination
          ....
          onChangePage={props.setBrokersListPage}
        />

The method setBrokersListPage automatically gets event and page objects . I want to pass an additional prop as a parameter . Something like

          onChangePage={props.setBrokersListPage(props.otherParam)}

And the otherParam will be the third param of setBrokerListPage . Is it possible ?

I tried using @omri answer but the default passed params are getting broken . Page is not recognized any more if I do this :

setBrokersListPage :

export const setBrokersListPage = (event, page,brokersListFilter) => {
    return getBrokers(page,brokersListFilter)  
}

And onChangePage :

onChangePage={() => props.setBrokersListPage(props.brokersListFilter)}

Yes it is possible, you can reach it by passing it as a function:

onChangePage={() => props.setBrokersListPage(props.otherParam)}

Or, instead of rendering a new function each time, you can do:

setBrokersListPage = () => {
    props.setBrokersListPage(props.otherParam)
}

onChangePage={this.setBrokersListPage}

You need to define your funcation so that on getting some arguments it can return another function like this.

setBrokersListPage = (args) => (event) => {
    // logic with args, event
}

Now do like:

onChangePae={props.setBrokersListPage(props.otherParam)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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