作者:徐新nina | 来源:互联网 | 2022-12-09 19:19
例如,这是我的观点:
现在我想在每次点击时将图像更改plus.png
为minus.png
.
因此,单击一次:minus.png => plus.png
,再次单击:plus.png => minus.png
,依此类推.我该怎么做?
1> Sagiv b.g..:
这可以通过简单的切换处理程序来实现:
const imagesPath = {
minus: "https://images.vexels.com/media/users/3/131484/isolated/preview/a432fa4062ed3d68771db7c1d65ee885-minus-inside-circle-icon-by-vexels.png",
plus: "https://cdn3.iconfinder.com/data/icons/glypho-generic-icons/64/plus-big-512.png"
}
class App extends React.Component {
state = {
open: true
}
toggleImage = () => {
this.setState(state => ({ open: !state.open }))
}
getImageName = () => this.state.open ? 'plus' : 'minus'
render() {
const imageName = this.getImageName();
return (
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(, rootElement);