Support component that does not import React but that still have props
See original GitHub issueI have a component that does not import React at all (not required)
// @flow
import { Component } from "react"
type Props = {
initialState?: State,
children: (
state: State,
setState: (state: State) => void,
) => ReactElement,
}
type State = {
[key: string]: any,
}
export default class Playground extends Component<void, Props, State> {
state: State;
constructor(props: Props) {
super(props)
this.state = (typeof props.initialState === "object")
? props.initialState
: {}
}
handleStateChange(state: State) {
this.setState(state)
}
render(): ReactElement {
return (
this.props.children(
this.state,
(state) => this.handleStateChange(state),
)
)
}
}
When I use this component, I got a “React is not defined” error. I suspect that babel-plugin-flow-react-proptypes replace the Props by React.PropTypes
even if React is not in the current scope.
Issue Analytics
- State:
- Created 7 years ago
- Comments:14 (6 by maintainers)
Top Results From Across the Web
How passing props to component works in React
Master how to pass props (properties) to components in React with this useful beginner's guide, including demo code.
Read more >You don't always need to import React - DEV Community
In this case, React object is not used anywhere so you can leave out the import statement (but still is a valid component)....
Read more >React.Component
Use shouldComponentUpdate() to let React know if a component's output is not affected by the current change in state or props. The default...
Read more >Can't import react component into a gatsby starter component
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: ...
Read more >How To Customize React Components with Props
Your custom components can use props to display data or use the data ... props do not match the type the code will...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think I already fixed it. Pushed as 0.5.0.
Let me know if it works for you.
@MoOx it looks like you have some solution for defining
children
- can you comment over at #48? What does yourReactElement
type look like?