[英]React-Bootstrap TypeScript - How to create a ImageButton
Long time I haven't come on SO to post a question but today... I am stuck on a new story and require your help!很长一段时间我没有来发布问题,但今天......我被困在一个新故事上,需要你的帮助!
I am trying to reproduce a design (html/css/jquery) (that is way to hard to maintain) into Bootstrap using react-bootstrap
.我正在尝试使用
react-bootstrap
将设计(html/css/jquery) (这是难以维护的方式)复制到Bootstrap 中。
I have the given button on my original website:我的原始网站上有给定的按钮:
I would like to reproduce it with the react-bootstrap
library.我想用
react-bootstrap
库重现它。 However, I am kind of stuck as I don't understand one point or two.但是,我有点卡住了,因为我不明白一两点。
I have the following component:我有以下组件:
image_button.tsx image_button.tsx
import * as React from "react";
import { Button, Image } from "react-bootstrap";
import { ImageProps } from "react-bootstrap/Image";
import { ButtonProps } from "react-bootstrap/Button";
export interface ImageButtonProps {
hoverSrc: string;
}
export interface ImageButtonState {
}
export class ImageButton extends React.Component<ImageProps & ButtonProps & ImageButtonProps, ImageButtonState> {
render() {
return <Button active={this.props.active} block={this.props.block} variant={this.props.variant} size={this.props.size} type={this.props.type} href={this.props.href} disabled={this.props.disabled} target={this.props.target} className={`image-button`}>
<Image src={this.props.src}/>
</Button>
}
}
export default ImageButton;
Note: My ultime goal would be to have a generic component that takes as props all the button/image props (and apply them) as well as ImageButton props that can be
:hover { src: /path/to/media.png }
注意:我的最终目标是拥有一个通用组件,它将所有按钮/图像道具(并应用它们)以及可以是
:hover { src: /path/to/media.png }
ImageButton 道具作为道具
Thanks for any help around the subject :) I know the question is quite wide and not focus on a simple/precise point :)感谢您对这个主题的任何帮助:) 我知道这个问题很广泛,而不是专注于一个简单/精确的点:)
Best,最好的事物,
Max最大限度
since the addition of react hooks people have been moving into functional components instead of Objects.由于添加了 react hooks,人们一直在转向功能组件而不是对象。
an easy way to map all properties is using the spread operator so something like this映射所有属性的一种简单方法是使用扩展运算符,所以像这样
function ImageButton({src, ...props}) {
return <Button {...props}>
<Image src={src} />
</Button>
}
export default ImageButton
also you can use css along side reactjs by importing the css file import './style.css'
您也可以通过导入 css 文件
import './style.css'
在 reactjs 旁边使用 css
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.