GithubHelp home page GithubHelp logo

Comments (6)

hcrufio88 avatar hcrufio88 commented on September 26, 2024 2

same problem here.

from react-router-bootstrap.

raffaeler avatar raffaeler commented on September 26, 2024 2

Any forecast on the publishing date?
I see the last published release from 2y ago.

from react-router-bootstrap.

kyletsang avatar kyletsang commented on September 26, 2024 2

This is published now

from react-router-bootstrap.

reinard avatar reinard commented on September 26, 2024 1

I was able to reproduce this issue with react & react-dom set to version 18.2.0.

This simple PR resolves the issue: #318

from react-router-bootstrap.

Anonyfox avatar Anonyfox commented on September 26, 2024

Quick solution for anyone waiting for an update, drop this standalone component into your project and use it instead (works with typescript/react18):

import React, { ReactElement, CSSProperties, MouseEvent } from 'react';
import { useHref, useLocation, useMatch, useNavigate } from 'react-router-dom';

interface LinkContainerProps {
  children: ReactElement;
  onClick?: (event: MouseEvent) => void;
  replace?: boolean;
  to: string | { pathname: string };
  state?: object;
  activeClassName?: string;
  className?: string;
  activeStyle?: CSSProperties;
  style?: CSSProperties;
  isActive?: ((match: any, location: any) => boolean) | boolean;
}

const isModifiedEvent = (event: MouseEvent) =>
  !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);

export const LinkContainer: React.FC<LinkContainerProps> = ({
  children,
  onClick,
  replace = false,
  to,
  state,
  activeClassName = 'active',
  className,
  activeStyle,
  style,
  isActive: getIsActive,
  ...props
}) => {
  const path = typeof to === 'object' ? to.pathname || '' : to;
  const navigate = useNavigate();
  const href = useHref(typeof to === 'string' ? { pathname: to } : to);
  const match = useMatch(path);
  const location = useLocation();
  const child = React.Children.only(children);

  const isActive = !!(getIsActive
    ? typeof getIsActive === 'function'
      ? getIsActive(match, location)
      : getIsActive
    : match);

  const handleClick = (event: MouseEvent) => {
    if (children.props.onClick) {
      children.props.onClick(event);
    }

    if (onClick) {
      onClick(event);
    }

    if (
      !event.defaultPrevented && // onClick prevented default
      event.button === 0 && // ignore right clicks
      !isModifiedEvent(event) // ignore clicks with modifier keys
    ) {
      event.preventDefault();

      navigate(to, {
        replace,
        state,
      });
    }
  };

  return React.cloneElement(child, {
    ...props,
    className: [
      className,
      child.props.className,
      isActive ? activeClassName : null,
    ]
      .join(' ')
      .trim(),
    style: isActive ? { ...style, ...activeStyle } : style,
    href,
    onClick: handleClick,
  });
};

from react-router-bootstrap.

raffaeler avatar raffaeler commented on September 26, 2024

@Anonyfox If I understand correctly, this should be done for all the components which looks like a huge overkill.

from react-router-bootstrap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.