I've created two components in React, <Search />
and <List />
that both are children of <App />
.
When a button in <Search />
is clicked, I want to fetch something from an API and display the results in <List />
. While I got this working by doing the fetch inside <App />
and passing the response as prop to <List />
, I would prefer to encapsulate the fetch inside <List />
.
Unfortunately I am having a hard time finding a way to do this. 'The React way' probably would be to do this via some clever prop passing, but I haven't found a neat way to do this – even a 'shouldFetch' boolean would need to be reset after the fetch which seems cumbersome and would trigger unnecessary renders.
This answer uses refs
for something similar which might work, but actually I am a bit hesitant to try it since refs
seem to be a bit dirty, according to the React docs, as they "imperatively modify a child outside of the typical dataflow".
How can I instruct my <List />
component to do something after a button in <Search />
has been clicked?
If required I can supply code – but hoping this question is simpler than it seems to me.
` should be perfectly happy with just receiving and displaying data. – Omri Aharon Jan 05 '17 at 20:55
`, so basically `