简体   繁体   English

如何在reactjs中禁用链接?

[英]How to disable a link in reactjs?

I have this in my reactjs app: 我在我的reactjs应用程序中有这个:

import Link from 'react-router/lib/Link'

Been trying to disable this link but this does not have the desired effect: 一直试图禁用此链接,但这没有达到预期的效果:

<Link disable={true}/>

It just renders it invisible. 它只是让它变得不可见。 How can I disable( based on a condition) the reactjs Link? 如何禁用(基于条件)reactjs Link?

Contain many issues on react-router , there is no support disabled attribute in Link Component, so you can try some with this issue : 包含react-router上的许多问题, Link Component中没有支持禁用属性,因此您可以尝试使用此问题

1. onClick event 1. onClick事件

Use preventDefault() to handle onClick event. 使用preventDefault()来处理onClick事件。

/* YourComponent.js */
class YourComponent extends React.Component {
  render() {
    return (
      <Link onClick={e => e.preventDefault()} />
    );
  }
}

2. CSS's pointer-events attribute 2. CSS的pointer-events属性

/* YourComponent.js */
class YourComponent extends React.Component {
  render() {
    return (
      <Link className='disabled-link' />
    );
  }
}

/* css file */
.disable-link {
  pointer-events: none;
}

or you can use inline style 或者你可以使用内联样式

/* YourComponent.js */
class YourComponent extends React.Component {
  render() {
    return (
      <Link style={{ pointerEvents: 'none' }} />
    );
  }
}

What I used was method 2, it's more clearly for me on my project. 我使用的是方法2,在我的项目中对我来说更清楚。

You could conditionally render something that looks like a disabled link based upon some state. 您可以根据某些状态有条件地呈现看起来像已禁用链接的内容。

For instance in typescript: 例如在打字稿中:

export interface Location {
  pathname: string; 
  search: string;
  state: any;
  hash: string;
  key ?: string;
}
interface LinkProps {
  to: string | Location
  replace?:boolean
}
interface DisableLinkProps extends LinkProps {
  enabled: boolean
  linkText:string
}
export class DisableLink extends React.Component<DisableLinkProps, undefined> {
  render() {
    var element= this.props.enabled ? <span className="disableLinkDisabled">{this.props.linkText}</span> : <Link to={this.props.to} replace={this.props.replace}>{this.props.linkText}</Link>
    return element;
  }
}

interface DemoClassState {
  linkEnabled:boolean
}
export class DemoClass extends React.Component<undefined, DemoClassState> {
  constructor(props) {
    super(props);
    this.state = { linkEnabled:false }
  }
  toggleLinkEnabled = () => {
    this.setState((prevState) => {
        return {
            linkEnabled: !prevState.linkEnabled
        }
    });
  }
  render() {
    return <div>
        <DisableLink enabled={this.state.linkEnabled} to="/somewhere" linkText="Some link" />
        <button onClick={this.toggleLinkEnabled}>Toggle link enabled</button>
        </div>
  }
}

This is actually a bit tricky. 这实际上有点棘手。 And maybe even somewhat ill-advised. 甚至可能有点不明智。

https://css-tricks.com/how-to-disable-links/ https://css-tricks.com/how-to-disable-links/

The route (no pun intended) I took was to not render the link. 我采取的路线(没有双关语)是不呈现链接。

I composed a new component from react-router's Link . 我从react-router的Link组成了一个新组件。

import React from 'react';
import { Link } from 'react-router-dom';

export default function ToggleableLink(props)  {
    const { disabled, ...rest } = props;
    return disabled ? props.children : <Link {...rest}>{props.children}</Link>;
}

Usage: 用法:

<ToggleableLink disabled={!showTheLink}>Foobar</ToggleableLink>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM