1

I have a search text box I need to get the value onchange send the request to API when I use the normal event.target method it shows error. how to rectify it as in onchange I need to call a function with some arguments so I cannot go by ease.

my text box is :

<input className="ReactSearchBox" name="search" placeholder="Search Doctors" 
          onClick={() => {
            this.onProviderListing(this.state.skip,0);
            this.onEnable();
                  }}
          onChange={() =>this.onSearchProvider(this.state.skip,0)} />

my function where i need the onchange value is:

onSearchProvider(nSkip,nLimit,e){
      this.setState({
        limit:nLimit,
        skip:nSkip,
        listing: this.state.listing,
        searching: !this.state.searching
      })
      //console.log(nlimit);
      var headers = {
        "Content-Type":"application/json",
        "AccessToken":localStorage.TOKEN,
      }
      var _calObj = {initiatorId:localStorage.userid,visitType: "office", skip:nSkip,limit:"5", includeOfflineProviders:"true",searchQuery:"lo"}

I need to give my input values in search query onchange correspondingly, sort it out plz.

3 Answers 3

1

You're not passing event from input. Change the onChange prop to:

       <input className="ReactSearchBox" name="search"
          placeholder="Search Doctors" 
          onClick={() => {
            this.onProviderListing(this.state.skip,0);
            this.onEnable();
                  }}
          onChange={(e) =>this.onSearchProvider(this.state.skip,0, e)}
        />

onSearchProvider(nSkip,nLimit,e){
  const value = e.target.value; // text box value 
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can update the onChange in jsx by passing the e event object also like:

onChange={(e) => this.onSearchProvider(this.state.skip,0,e)}

and in onSearchProvider you can access it like:

onSearchProvider(nSkip, nLimit, {target}){
   // you can see search box text here on change
   console.log(target.value) 
}

Comments

0

You don't need to pass state values on onChange method call.

State will be consistent throughout component.

<input className="ReactSearchBox" name="search" placeholder="Search Doctors" 
     onChange={() =>this.onSearchProvider(0)}
/>

And you can get value from event of that input as event.target.value

onSearchProvider(nLimit,e){
  // access your state values directly here like this.state.skip
  const searching = e.target.value;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.