-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using setState inside a "function as child component" generates a warning about functions not being valid children #11116
Comments
I’m not sure I understand what you’re trying to do, but it seems like you‘re calling This error makes it pretty clear: Even smaller reproduction: https://codesandbox.io/s/wq69qyvrx8 |
Yes, the problem is that If your import React from 'react'
import { render } from 'react-dom'
import Fetch from './Fetch'
class App extends React.Component {
_renderChildren = (data) =>
<span>{JSON.stringify(data)}</span>
render() {
return (
<Fetch
path="https://unsplash.it/list"
render={this._renderChildren}
/>
);
}
}
render(<App />, document.getElementById('root')) Here is a working sandbox: https://codesandbox.io/s/48rrpxq33x |
Thanks @Tom-Bonnike @gaearon for quick answers. That's actually a little bit different from what I was trying to achieve there as you saw. The idea was to fetch data with |
The solution in your case would be to modify the render callback to pass that data down to any components that need it. I know it’s a bit counter-intuitive but since you went with this pattern, there’s no need to bring data “up” now in your component. Just tell |
Thanks for your advice. That's exactly what I'm doing now: <Fetch path="/api/route">
{(data) => <MyComponent content={data} />}
</Fetch> I'm just exploring different patterns to see if there is another approach that could be more interesting. Anyhow thanks again for your time! |
One way to think about it is that for every piece of data there should be only one component that holds it in the state. So you need to pick: either it's Fetch (and you can only use what it gives you via the render prop), or it's App (and in this case you need a different Fetch API that would call |
I'm leaning toward the second option, just to see what I can achieve with it. In the first place, I wanted to lift my state up (using On another note: do we also have access to |
I think it's actually OK. Think Fetch is like an input. The mistake was in trying to set the state in the render phase. Instead you could call |
Not sure what you mean. I suggest you to have Fetch accept onChange as a custom prop, and then have it call it when the data is fetched. |
Here's an example of the second approach I was referring to: https://codesandbox.io/s/j7jlyp07l9 It's very close to what you did originally, but without the state duplication or the update-state-during-render issue. Hope this helps! |
Okay! I got it now. I thought you were referring to the The custom It definitely solved my problem, thank you! |
Happy to help! |
Do you want to request a feature or report a bug?
Reporting a bug.
What is the current behavior?
Context: React Native app, but the error isn't related to the renderer
I'm using a handcrafted
<Fetch path="/api/route" render={this.renderChildren} />
component which fetches data and passes it to a ''function as child component", which is rendered. However when usingsetState
to save the call result, I get a pretty interesting error as you can see:👇 gives you 👇
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template for React 16: https://jsfiddle.net/Luktwrdm/, template for React 15: https://jsfiddle.net/hmbg7e9w/).
I tried to remove all the business logic and reproduce the error from my React Native app. Here is a CodeSandbox which fully reproduce it: https://codesandbox.io/s/pmo6okkz1x.
What is the expected behavior?
I expected not to have this error.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
From what I know: this error occurs on any React renderer (got it on React Native in the first place), and started the project with React 15.5.
Related issues
Might be related to: #11081, #11086.
Ping: @gaearon
The text was updated successfully, but these errors were encountered: