gpt4 book ai didi

reactjs - Reactstrap工具提示动态ID

转载 作者:行者123 更新时间:2023-12-03 13:41:05 26 4
gpt4 key购买 nike

我正在开发一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com