0

Is it alright to make conditional rendering inside of render function of <Route /> component of React-router? Kind of this:

<Route exact path="/form" render={props => (
  this.props.dataLoaded && <Form {...props} />
)} />

The idea is ... I dont want to mount the Form component until some data in parent component are loaded.

I have that data in redux store, so basically I could access it inside of the child component. The problem is that I would have to check the changes and make some additional stuff in order to achieve what I need.

The conditional rendering seems to me as much simpler solution. I just havent seen it anywhere yet so Im not sure whether it is technically right solution.

2
  • You could do it this way or you could create a parent component that controls the logic and conditionally rends <Form>. Then link the parent component to the route. Commented Jul 25, 2018 at 15:50
  • 1
    From a technical point of view, you can use such an approach. Please make sure that it's ok from the user perspective - you could use some loading state for that. Commented Jul 25, 2018 at 16:59

1 Answer 1

1

I have two solutions for you and you can choose. first, you can render some component if data loaded otherwise you can load for example some other like loading component

<Route exact path="/form" render={props => (
     this.props.dataLoaded === true? <Form {...props} /> : <Loading {...props}/>
)} />

so if dataLoaded is equal to true it renders <Form> otherwise render <Loading>. Another solution is completely rendered router if data loaded:

{this.props.dataLoaded && <Route exact path="/form" render={props => <Form {...props}/>} /> }

in this scenario <Form> is accessible only if dataLoaded is true

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.