score:1
Accepted answer
You can integrate componentDidUpdate()
and use it to render your child-components in a somewhat synchronous flow.
Let's say the structure of your Parent Component should look something like the following:
Parent
class Parent extends React.Component{
state = {
renderChildren: false
}
async componentDidMount() {
await this.props.getEventTypes();
}
componentDidUpdate(prevProps){
if(this.props.yourUpdatedReducer !== prevProps.yourUpdatedReducer){
this.setState({
renderChildren: true
})
}
}
render(){
const { renderChildren } = this.state
return(
{ renderChildren ? <Child/> : "Loading" }
)
}
}
- You want a key in your state that determines whether you should render the Child component.
- In
componentDidMount()
, you call the action-creator function, when it completes, you get updated props. - With updated props, you trigger
componentDidUpdate()
, where you check the values in your reducer. If the values are different that means you got the updated data from your database, so everything has loaded. - Great, so now you want to mount your Child component, so you update-state, setting renderChildren to true, thus re-rendering the Parent component. Now Child gets rendered and should behave as expected.
score:2
You can try like this
Set isDataLoaded
when data is available.
Use ternary operator for conditional rendering.
In you render
return(
<>
....
{ isDataLoaded ? <ChildComponent /> : null }
....other sutff
</>
);
Use can also use the &&
operator
return(
<>
....
{ isDataLoaded && <ChildComponent /> }
....other sutff
</>
);
Source: stackoverflow.com
Related Query
- How can I make sure a React parent component loads data before its child component mounts?
- How to make React.js fetch data from api as state and pass this state data from its parent to child component
- How can I make a parent component be the label of its child component?
- How can I Send data to another Component without using Parent and Child Relation in React
- How to stop React child component from loading before data from fetch call in parent is passed to it
- How to pass data from child component to its parent in ReactJS?
- how to make material data grid width to fill the parent component in react js
- How can my child component update my parent component in React JS?
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- How can I make an axios get request wait before rendering my react component
- How do I send data from child component to parent component in React
- how to pass a child component's validated data (as a formik form) to its parent component and handle form submission in parent
- How to make sure API calls completes before another page loads in React
- How to make a react component that can be rendered with no data and later rendered with data?
- How can reset a React child component back to original state after it updates state in the parent
- How can I make transient/one-off state changes to a React component from one of its ancestors?
- How to make React listen to event in Child component first instead of Parent component?
- How can I send data from a child to a parent functional component in ReactJS?
- How can i pass data by onclick chilld component to parent component react
- With a React Hooks' setter, how can I set data before the component renders?
- React : How to make sure that requested data are available before rendering?
- How to access data in a child react component from a parent react component
- How can i pass data from child component to parent component which in above second level by using function base in React?
- How to transfer data from react child component to parent component
- React snapshot test with Jest, how can a child component calls a function from parent component?
- How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js?
- How can we make sure other <ListItem> components as collapsible when one <ListItem> component get expands under <List> react material ui component?
- how does elementUI make child component pass data to parent component?
- How can I make each child of component has a state? react redux
- How can I send data from a child component to a parent of a parent of a parent (react)?
More Query from same tag
- Flow required module not found
- React passing props from const to child component
- Action called in componentWillReceiveProps goes in an infinite loop.
- reactjs - how to set style of border?
- I'm getting an undefined error in my project made with React
- React: Invalid prop type Link when using JSX
- Redux initialize data on refresh delay
- fetch not returning data in react
- How to render components side by side
- hide autocomplete (suggestions ) list when scrolling in antd modal / react
- Show button when 2 inputs are full and valid in react native
- Javascript: cannot enter zeros after a decimal point
- Material UI Menu with subitems won't close whole menu
- How can you implement a React component in Rust?
- How to make this available inside render function
- Grab theme from local storage before page is rendered in react
- Child component is not updating when parent state changes
- Can't generate base64 string from react signature canvas in react
- Separating Route into two different files in react-router-dom
- How to detect if user has blocked camera?
- How to get returned data from one function in react
- Deployed React App to IONOS - 404 on page refresh
- React component with Three.js canvas is working but NOT every time
- How can I sort an RSS feed via .isoDate and keep the feed's contents connected (title and link)?
- React - Redux - Connect data fetched to Component
- Get return variable from React.Children
- formData.append is null value in reactjs in uploading file
- Updating an array with useState in a form
- How can I set/fake the proptype of a component
- How to rewrite class component to React Functional?