2

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} /> 
15
  • Your component(SingleDatePicker) has the onClick event itself? Commented Jun 6, 2021 at 8:45
  • no, it doesn't .. Commented Jun 6, 2021 at 8:49
  • So, what do you want to do with SelectDatePicker when img clicked? Commented Jun 6, 2021 at 8:53
  • I want, when the img is clicked to happen the same thing as if I would click on the SelectDatePicker Commented Jun 6, 2021 at 8:56
  • You can add a new state to your states. So on img click event change the state and when the state change and your component render again, you have your new state and you can do everything you want with that. Commented Jun 6, 2021 at 9:11

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.