Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Way to detect which component is clicked

In my jsx page I have two identical search input components used to filter both company and users data. Is there a way to detect which button has been pressed using React?

<SearchInput label={'Cerca Azienda'} company={company} users={users} setSearchResults={setSearchResults} />

<SearchInput label={'Cerca Utente'} company={company} users={users} setSearchResults={setSearchResults} />

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Unfortunately, that is not really possible. However, I’d like to propose an alternative method – a function which returns another function:

const setSearchResults = (name) => {
    return () => {
        console.log('Setting search results for company', name)
    }
}

Then, you can simply use it like this:

<SearchInput label={'Cerca Azienda'} company={company} users={users} setSearchResults={setSearchResults('Cerca Azienda')} />

<SearchInput label={'Cerca Utente'} company={company} users={users} setSearchResults={setSearchResults('Cerca Utente')} />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading