Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onclick event for Imported component in react?

Tags:

reactjs

I have imported a component from a different file and I want to reset my timer if I click on the imported component's elements. Is there a way to tackle this issue or should I write both components in single jsx ?

import {SampleComponent} from "../SampleComponent";
<div>
   <SampleComponent  onClick = {?????????}/>
 </div>
like image 892
Feroz Avatar asked Feb 07 '19 03:02

Feroz


People also ask

How do you call a component onClick in react?

The React onClick event handler enables you to call a function and trigger an action when a user clicks an element, such as a button, in your app. Event names are written in camelCase, so the onclick event is written as onClick in a React app. In addition, React event handlers appear inside curly braces.

How do I import onClick into react JS?

Example: Pass a Button Value to an Inline Function Notice the value e that's returned from the onClick event handler: import React from 'react'; function App() { return ( <button value="hello!" onClick={e => alert(e. target. value)}> Click me!

Can onClick be used with DIV in react?

To set your mind at ease, the onClick event does work with divs in react, so double-check your code syntax.

What is onClick event in react?

The onClick event is used to listen for click events on DOM elements. We also reviewed some common use cases of the onClick event handler in functional components, such as updating the state, calling multiple functions, and using synthetic events.

What is the event handler in react?

In React, the onClick handler allows you to call a function and perform an action when an element is clicked. onClick is the cornerstone of any React app. Click on any of the examples below to see code snippets and common uses: Call a Function After Clicking a Button; Call an Inline Function in an onClick Event Handler

Why do we use props for onClick event in listcomponent?

We use props to determine the function that gets called when the onClick event fires, and the text within the button. We do this also for the ListComponent’s inner text. This means that we have to set these attributes in the function that calls Button, App.js: We get the following render in the window:

How do I add an event listener to a React component?

When using React you should generally not need to call addEventListener to add listeners to a DOM element after it is created. Instead, just provide a listener when the element is initially rendered. When you define a component using an ES6 class, a common pattern is for an event handler to be a method on the class.


Video Answer


3 Answers

What you can do here is,

import {SampleComponent} from "../SampleComponent";

<div onClick={??????}>
   <SampleComponent/>
</div>

Or you can pass the function from your parent component and add click event on top node of the child component.

<div>
   <SampleComponent onHandleClick={() => ??????}/>
</div>
like image 77
prabin badyakar Avatar answered Oct 20 '22 16:10

prabin badyakar


If you want to call a function in parent component, whenever an event (such as in your case an onClick event) occurs in a child component, you will need to pass the parent function as a props. Here's what it will look like:

class ParentComponent extends React.Component {
 handleClick = () => { ... }

 render {
  return (
   <SampleComponent onClick={this.handleClick} />
  )
 }
}

And here is how your SampleComponent will be:

class SampleComponent extends React.Component {

 render {
  return (
   <div onClick={this.props.onClick}> //to call the function on whole component
    <button onClick={this.props.onClick}>Click Me</button> //to call the function on a specific element
   </div>
  )
 }
}

What I have understand so far from your question is that you want to call a function in SampleComponent whenever a click event occurs on it (on SampleComponent).

To do this, here is how your SampleComponent will look like :

class SampleComponent extends React.Component {
.
.
render() {
 handleClick = () => { ... }

 return (
  <div onClick={this.handleClick}>
    ...
  </div>
 )
}

Note: For this you don't need to add onClick in parent.

like image 32
Nikhil Thakur Avatar answered Oct 20 '22 17:10

Nikhil Thakur


resetTimerHandler = (timer)=>{
    timer = 0; or this.setState({timer: 0}) // how ever you are defining timer
}
render(){
    let displayTimer;
    this.updateTimer(displayTimer)// However you are updating timer
    return(
        <SampleComponent onClick={this.resetTimerHandler.bind(this,displayTimer)} />)

Without knowing how you are updating your timer I can't really give a specific answer but you should be able to apply this dummy code.

like image 1
Mernlin Avatar answered Oct 20 '22 16:10

Mernlin