0

I have an input field in one of my React Component which will take the inputs which will be email id's of users. I have a list of email id's saved in the server.So,when I type something in the input field, I want to have the suggestion of the email id's to appear below the input field from the currently existing list.So, what I have come up with is that I should use the localeCompare(string) function from javascript where I will have to use some condition like :

input_string.localeCompare(existing_string)

So,is this the correct way or is there some other way to proceed? And do I have to create a new component for displaying the suggestion below the input?

NOTE: I want my search to work in a similar manner like how google search works when we type some input in the search bar, the suggestion similar to the search string appears below.

4
  • There are search suggestion npm packages out there that will do things like this for you Commented Apr 24, 2018 at 4:53
  • @Andrew I was trying to figure out if there is a way where i can create a component on my own that will do the same filtering work so that I can use it for that purpose. Commented Apr 24, 2018 at 5:13
  • 1
    I suppose if it's for self enrichment, by all means. But usually you don't need to reinvent the wheel Commented Apr 24, 2018 at 5:19
  • @Andrew yes,you are correct.It's for self enrichment and learning. And I will definitely try out the packages that are already present. Thanks for suggesting :) Commented Apr 24, 2018 at 5:26

2 Answers 2

1

The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

You may use filter, and then you may use Regular Expression. Here's an example using includes method:

render() {
  const emailList = [
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]'
  ];
  const lookupList = emailList.filter(email => email.includes(inputRefValue));
  return <div>{ lookupList.map(el => el) }</div>
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer.Just let me try it out.I will let you know if it works as expected. :)
Glad to help you.
1

Comparison on the client side is not a good solution. Suppose, you might have hundreds of thousands of emails on your server, there is no way you can load all the data. The way it's done is you call(GET request to the server) the server in every new keystroke from the client.

I have tried to show the process in the block diagram as below If that makes sense.

enter image description here

You can look at this tutorial.

1 Comment

Thanks for the answer.I will definitely try it out the way you suggested. For now, I was trying to get the result for a small list of emails.Once I try to work for a bigger set of data,I will try to follow your approach.Thanks for your help. :)

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.