简体   繁体   English

React-Bootstrap TypeScript - 如何创建一个 ImageButton

[英]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;
  • The first question that comes to my mind is: Do I have at least the right approach?我想到的第一个问题是:我至少有正确的方法吗?
  • If I do, then how or what would be the best way to pass all the props to my sub-component?如果我这样做了,那么将所有道具传递给我的子组件的最佳方式是什么? As you see, I use a "mapping" approach where I set all the values which might not be the best way to do it...如您所见,我使用“映射”方法设置所有可能不是最佳方法的值......
  • Let say I want to customize it (when hover, change the background image?), how can I apply styles to my component?假设我想自定义它(悬停时,更改背景图像?),我如何将样式应用于我的组件?

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.

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