I'm using ReactJs together with Flux. So I have views, action creators, a dispatcher and stores. I created a reusable search component that uses a 'searchActionCreator' (which calls a search API). Finally the 'searchStore' will receive the search results and will emit an event that the search component will act upon to display the search results.
The front-end developer who uses this component only needs to set some properties to define what to search for and pass a method (delegate) that will be called when a user selects one of the search results. I do not want to pass the search results to the search view from a parent component, because that would make it a little bit more complex to re-use the search component for something else.
Eveything works fine, as long as there only is one search component in every browser window. Because every search component registers an event handler with the searchStore, every search component will be triggered to display the search results when one of the search components made a call to a search API.
I can think of some options myself how to prevent this behaviour. E.g. I could set the state of a search component to 'waiting' after creating and firing the search action until the component received the results. Only components that have a 'waiting' state need to process a 'resultsReady' event from the searchStore. Another option would be to pass a unique ID of the search component togeter with the search query to the search action creator. The search store would need to supply this ID to all requesting search component, so that the search component can compare this ID to its own unique ID.
I can't imagine that I'm the only one person who encounters this challenge and that there must be some sort of standardized solution. I already read a good article on asynchronous requests, but haven't found an answer to this question in this article either. Any suggestions would be greatly appreciated!