reactjs - Reactstrap工具提示动态ID

标签 reactjs bootstrap-4 reactstrap

我正在开发一个 React 应用程序并使用 Reactstrap。

我正在使用reactstrap的工具提示组件,它需要一个目标属性,即目标元素的id值。这个id是动态生成的,似乎reactstrap工具提示不喜欢它。

组件看起来像:

MovieCard.jsx

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Col, Card, CardImg, CardBody, CardTitle, CardSubtitle, CardText, Button, Tooltip } from 'reactstrap';
import { LimitedTextTitle } from '../custom-styled/CustomStyledComponents';

class MovieCard extends Component {  

  constructor (props) {
    super(props);
    this.state = {
      open: false
    };
    this.toggle = this.toggle.bind(this);
  }

  toggle () {
    this.setState({
      open: !this.state.open
    })
  }

  render () {
    const { imdbID, Title, Year, Rated, Plot, Country, Poster } = this.props.movie;

    return (
  <Col md="4">
    <Card>
      <CardImg
        top
        width="100%"
        src={Poster}
        alt="blah"
      />
    </Card>
    <CardBody>
      <CardTitle>
        <LimitedTextTitle id={imdbID}>
          {`${Title} - (${Year})`}
        </LimitedTextTitle>
        <Tooltip placement='top' target={imdbID} isOpen={this.state.open} toggle={this.toggle}>
          {Title}
        </Tooltip>
      </CardTitle>
      <CardSubtitle>{`Rated: ${Rated} Country: ${Country}`}</CardSubtitle>
      <CardText>{Plot}</CardText>
      <Button>Read More</Button>
    </CardBody>
  </Col>
);
  }
}

MovieCard.propTypes = {
  movie: PropTypes.object.isRequired // eslint-disable-line
};

export default MovieCard;

有什么建议吗?

react 版本16.2.0

reactstrap 5.0.0-alpha.4

最佳答案

正在处理类似的问题。

添加代码作为答案,因为我无法在上面添加评论...

希望它能帮助您或其他遇到此问题的人。

描述: 对动态生成的元素使用 Reactstrap 工具提示。

import React from 'react';
import ReactDOM from 'react-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Button, Tooltip } from 'reactstrap';

class App extends React.Component {
  state = {};

  toggle = targetName => {
    if (!this.state[targetName]) {
      this.setState({
        ...this.state,
        [targetName]: {
          tooltipOpen: true
        }
      });
    } else {
      this.setState({
        ...this.state,
        [targetName]: {
          tooltipOpen: !this.state[targetName].tooltipOpen
        }
      });
    }
  };

  isToolTipOpen = targetName => {
    return this.state[targetName] ? this.state[targetName].tooltipOpen : false;
  };

  render() {
    return (
      <div>
        {[1, 2, 3, 4, 5, 6].map((x, i) => (
          <div key={`div-${i}`}>
            <Button color="link" id={`btn-${i}`}>
              {x}
            </Button>
            <Tooltip
              placement="right"
              isOpen={this.isToolTipOpen(`btn-${i}`)}
              target={`btn-${i}`}
              toggle={() => this.toggle(`btn-${i}`)}>
              Hello world!
            </Tooltip>
          </div>
        ))}
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));

react :16.9.0

react 带:8.0.1

https://codesandbox.io/embed/angry-taussig-fup7i?fontsize=14

关于reactjs - Reactstrap工具提示动态ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48404813/

相关文章:

reactjs - 使用 HTTPS=true 运行 Docusaurus 会产生 ERR_SSL_PROTOCOL_ERROR

javascript - React - 语法问题

html - Bootstrap 4 - 导航栏扩展中缺少填充

css - Bootstrap 卡片边框厚度

javascript - 找不到模块 : 'cldr' in . ..\globalize\dist\globalize

html - 在 Material-UI 中调暗/禁用 div 的最佳方法?

javascript - auth.signOut() 或signOut(auth)。它们之间有什么区别或主要优点吗?

javascript - Bootstrap 4 网格列不假设最大高度

javascript - 使用键盘上的 Esc 键关闭模式弹出窗口

Reactjs - 无法使下拉菜单正确格式化