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

LinkContainer should handle null/undefined locations #257

Open
getaaron opened this issue Jun 21, 2020 · 0 comments
Open

LinkContainer should handle null/undefined locations #257

getaaron opened this issue Jun 21, 2020 · 0 comments

Comments

@getaaron
Copy link

Feature Request

LinkContainer should handle null or undefined locations by rendering the child component without modifications.

Why?

Imagine we have an array of model objects and want to render them as a table or list. However, sometimes only some of them have links associated. We'd like to do something like:

    <ListGroup>
      {steps.map((step) => {
        const shouldShowLink = !!step.link;

        return (
          <LinkContainer
            to={step.link}
            key={step.name}
          >
            <ListGroup.Item
              action={shouldShowLink}
              variant={step.completed ? "success" : null}
              disabled={step.disabled}
              props
            >
              {step.title}
            </ListGroup.Item>
          </LinkContainer>
        );
      })}
    </ListGroup>

In this case, when step.link is missing, we'd still like to render the ListGroup.Item component. However, LinkContainer complains that location is null and/or undefined, depending on what's passed in.

Possible Implementation

Haven't tested this, but I think it might be as simple as:

    // …snip…
     } = this.props;
+   
+   if (!to) {
+     return React.Children.only(children);
+   }
+   
     const href = history.createHref(
       typeof to === 'string' ? { pathname: to } : to
    // …snip…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant