ReactJS PreventDefault when already passing parameter

Tindra

My onClick button is calling the function getUserInput(i, event), however, since the onClick event is bound (bind) to (this, id), I can't understand how I will pass the over "event" parameter?

Function:

getUserInput = (i, event) => {
    event.preventDefault();
    const searchLocation = document.getElementById(i).value;
    this.state.locationArray[i].location = searchLocation;
    this.setState({
        locationArray: this.state.locationArray
    })
    this.createGrid();
}

Button:

<form className="form" role="search" autoComplete="off">
      <input id={id} className="searchBar" type="search" name="searchField" placeholder={filledArray[i].name}></input>
      <button onClick={this.getUserInput.bind(this, id)} value="submit" className="searchButton"><i className="fa fa-search"></i></button>
</form>
Shubham Khatri

.bind return a new function by adding the parameters passed to it at the beginning followed by the default parameters and binding the function to the correct context and hence when you write

onClick={this.getUserInput.bind(this, id)}

getUserInput receives id as the first parameter and the default parameters i.e event as the second so you don't need to pass it explicitly

A typical implementation of bind function would be

Function.prototype.bind = function(context){
    var that = this,
        slicedArgs = Array.prototype.splice.call(arguments, 1),
        bounded = function (){
            var newArgs = slicedArgs.concat(Array.prototype.splice.call(arguments, 0));
            return that.apply(context,newArgs);
        }
    bounded.prototype = that.prototype;
    return bounded;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Passing parameter in ReactJS onclick

preventDefault vs return false when it comes to reactjs

How to passing the parameter to event handle in reactjs

Passing parameter when pop view

Error when passing null parameter

ReactJS error when passing a function as a props

Passing a parameter to an event handler in the parent class from a child class ReactJS

"this" reference changing when passing it as parameter with RMI

An issue when passing a PHP parameter to a JavaScript function

An issue with a generic method when passing an array as a parameter

when passing parameter to function Return null in oracle?

Error when passing PHP parameter to SQL Query

onClick event fires infinite when passing a parameter

Error 424 when passing Range parameter

What is being copied when passing a string as parameter?

Getting [object] when passing parameter to javascript function

Passing parameter to a python class when initializing it asynchronously

Overloading function when passing lambda as parameter

Passing event parameter when clicking a div in React

Casting a pointer when passing it as an argument for void* parameter

Fatal error when passing parameter by reference

backbonejs filter data when passing parameter

Null reference exception when passing EditText as parameter

Why we need to pass module.exports as a parameter since we are already passing module as a parameter?

Passing parameter to SSRS url issue when parameter value have '&'

ReactJS form submit preventdefault not working

ReactJS: e.preventDefault() is not a function

PreventDefault() with named function and function parameter

If statement not working when passing props to functional component using Reactjs?