Skip to content
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

refactor(Route): simplify nested Ternary Conditional Operator #7220

Closed
wants to merge 3 commits into from

Conversation

izayl
Copy link

@izayl izayl commented Mar 27, 2020

It's hard to manage and understand use so many nested Ternary Conditional Operator.

So I refactor it to a more cleaner logic branch version.

@MeiKatz
Copy link
Contributor

MeiKatz commented Mar 27, 2020

To be honest: instead of defining the method each time, define it as a class method and pass it the props as arguments. Also we should get rid of every ternary operator. Maybe the solution is something like the following:

renderRouteChild(props) {
  if (!props.match) {
    if (typeof props.children !== "function") {
      return null;
    }

    if (__DEV__) {
      return evalChildrenDev(props.children, props, props.path);
    }

    return props.children(props);
  }

  if (props.children) {
    if (typeof props.children !== "function") {
      return props.children;
    }

    if (__DEV__) {
      return evalChildrenDev(props.children, props, props.path);
    }

    return props.children(props);
  }

  if (props.component) {
    return React.createElement(props.component, props);
  }

  if (props.render) {
    return props.render(props);
  }

  return null;
}

Now we can use it like this:

return (
  <RouterContext.Provider value={props}>
    {this.renderRouteChild({
      ...props,
      match: match,
      path: this.props.path,
    })}
  </RouterContext.Provider>
);

@izayl
Copy link
Author

izayl commented Mar 28, 2020

@MeiKatz You're right, Thank you for your advice

@MeiKatz
Copy link
Contributor

MeiKatz commented Mar 28, 2020

@izayl and now get rid of the remaining ternary operators ;)

@MeiKatz
Copy link
Contributor

MeiKatz commented Mar 28, 2020

@izayl Thank you 👍 I would approve your pull request but I am not authorized to do so. Maybe @timdorr?

@timdorr
Copy link
Member

timdorr commented Mar 28, 2020

Eh, I don't want to get into the whole philosophical debate of ternary vs if/then. I've stated before that this form is more readable, but I know Ryan and Michael disagree. It’s a pedantic argument, so I don’t think it’s worth having yet again. I’ll leave it up to them if they want to accept this.

@stale
Copy link

stale bot commented May 27, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
You can add the fresh label to prevent me from taking any action.

@stale stale bot added the stale label May 27, 2020
@stale stale bot closed this Jun 8, 2020
brophdawg11 pushed a commit that referenced this pull request Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants