appbaseio / reactivesearch

Search UI components for React and Vue
https://opensource.appbase.io/reactivesearch
Apache License 2.0
4.89k stars 466 forks source link

Number of result #356

Closed ymzoughi closed 6 years ago

ymzoughi commented 6 years ago

Issue Type: Question Platform: ReactiveSearch for Web

Description: Can i get the total number of result ?

metagrover commented 6 years ago

You can use onResultStats prop on results components. Refer docs here.

ymzoughi commented 6 years ago

but this function does return a result several times which does not allow me to save the value in my store with this.setState ({nbResult: total})

metagrover commented 6 years ago

I don't think it should affect you if the function is getting called several times (which happens because of component's internal rendering). You will always get relevant total count as the results are getting rendered.

The value of total changes as you keep filtering the results.

nestedifsdotcom commented 6 years ago

Hello, i added a this.setState in the function on the onResultStats prop and the whole application froze. this is the code used <ResultCard componentId="result" title="Résultat des recherches - نتائج البحث" dataField={"titre"} from={0} size={12} pagination={true} onResultStats={(time, total) => { this.setState({ time, total }); }} react={{ and: ["searchbox", "BiensCloud", "wilayas", "Transaction", "Biens"] }} onData={(res) => { return { image: res.image, title: res.titre, description: res.prix + " " + res.descr, prix: res.prix, url: res.site + res.lien } }} />

I can't find the error could you please help me find it? Thank you.

divyanshu013 commented 6 years ago

The reason this happens is because onResultStats is used in the render function to render the JSX returned. If setState is called in the render function it would cause the ResultCard to re-render, which would again invoke onResultStats and so on, thereby causing an infinite loop and break the app.

There is some level of customization possible using innerClass. onResultStats has been designed as a render function. If you wish to store the stats in state, one temporary solution is to check your state variables if they match total and time before updating state and returning null.

onResultStats={(total, time) => {
  if (this.state.total !== total) {
    this.setState({ total, time })
  }
  return null;
}

Note

This is not a good way to do things, we would have to think through the API to make it more extensible, but it should resolve your issue for the time being.

nestedifsdotcom commented 6 years ago

thank you very much for your answer

divyanshu013 commented 6 years ago

Opened a new issue for further discussions. Closing this one :)