Bogos Robert
Bogos Robert

Reputation: 233

How can I trigger a click event for a React component using his id?

I have a react component and I want to know how can I trigger a click event on it by clicking an image in my case. I tried with React's useRef(), but it doesn't work. Any idea how can I do this ? My code is bellow

import { SingleDatePicker  } from 'react-dates';

 <SingleDatePicker
    date={dateEnd} 
    onDateChange={dateEnd => handleDateEnd( dateEnd )}
    focused={this.state.focusedEnd}
    onFocusChange={({ focused }) => this.setState({ focusedEnd : focused })}
    id="dateEndSearchBar" 
    />

    <img
    src={calendarIcon}
    onClick={//here I want to trigger the click on SingleDatePicker}
    />

Upvotes: 2

Views: 1464

Answers (2)

Majid M.
Majid M.

Reputation: 4954

You can use onFocusChange prop in SingleDatePicker based on this link:

http://github.com/airbnb/react-dates/issues/639

Upvotes: 1

SuspenseFallback
SuspenseFallback

Reputation: 138

you could set a class for the component in state and modify it on the img's onClick

Upvotes: 1

Related Questions