reactivesearch icon indicating copy to clipboard operation
reactivesearch copied to clipboard

Number of result

Open ymzoughi opened this issue 8 years ago • 7 comments

Issue Type: Question Platform: ReactiveSearch for Web

Description: Can i get the total number of result ?

ymzoughi avatar May 08 '18 14:05 ymzoughi

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

metagrover avatar May 08 '18 14:05 metagrover

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})

ymzoughi avatar May 08 '18 15:05 ymzoughi

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.

metagrover avatar May 08 '18 15:05 metagrover

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.

nestedifsdotcom avatar Jun 04 '18 21:06 nestedifsdotcom

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.

divyanshu013 avatar Jun 05 '18 06:06 divyanshu013

thank you very much for your answer

nestedifsdotcom avatar Jun 05 '18 15:06 nestedifsdotcom

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

divyanshu013 avatar Jun 05 '18 17:06 divyanshu013