在React中通用化自定义按钮:自定义文本和背景图像

huangapple go评论66阅读模式
英文:

Generalizing custom buttons in React: custom text and background-image

问题

我尝试创建一个包含四个按钮的组。这些按钮几乎相同,只有背景图像和按钮上的文本不同。我可以为每个按钮创建一个组件,但我试图实现一种更通用的方法。我的母语是Java,我一直试图将组件视为对象。

我尝试了

ImgBtn.js
(这将充当构造函数)

import './App.css';
function ImgBtn(customImg, btnTxt) {
    return (
        <>
            <button style={{ backgroundImage: customImg }}>{btnTxt}</button>
        </>
    );
}

export default ImgBtn;

App.js
(在这里,我将调用四个ImgBtn的实例,现在只关注其中一个)

import ImgBtn from './ImgBtn';
import practRange from "./btn_img/p-range.png";

function App() {
    render(
        <div>
            {<ImgBtn customImg={practRange} btnTxt="sample" />;}
        </div>
    );
}

export default App;

App.css
(按钮的通用样式)

body {
    background-color: aqua;
}
button {
    font-size: 1rem;
    border: none;
    background-color: white;
    font-family: Arial;
    font-weight: bold;
    height: 200px;
    width: 200px;
    border-radius: 4px;
}
img {
    margin-top: none;
    padding-top: none;
}

我希望ImgBtn充当一个类似"构造函数"的角色,可以用来调用四个不同的按钮,但是我遇到了错误。我相当确定这都与我在React中参数(props?)如何工作的困惑有关。

谢谢。

英文:

I am trying to create a group of four buttons. The buttons are almost identical save the background-image and the text on the button. I could create a component for each button, but am trying to implement a more generalized approach. My native language is Java, I keep trying to think of components as objects.

I tried

ImgBtn.js
(This would act like the constructor)

import &#39;./App.css&#39;;
function ImgBtn(customImg, btnTxt) {
    return (
     &lt;&gt;
      &lt;button style = {{backgroundImage : customImg}}&gt;&#39;btnTxt&#39;&lt;/button&gt;
     &lt;/&gt;   
      );
  }

  export default ImgBtn;

**App.js **
(Where I would invoke the four instances of ImgBtn, just focusing on one for now)

import ImgBtn from &#39;./ImgBtn&#39;;
import practRange from &quot;./btn_img/p-range.png&quot;;


function App() {
render(
    &lt;div&gt;
   { &lt;ImgBtn(customImg: practRange, btnTxt: sample)/&gt;;}
   &lt;/div&gt;
);
}

export default App;

**App.css **
(The general styling for the buttons)

 body {
  background-color: aqua;
}
button {
  font-size: 1rem;
  border: none;
  background-color: white;
  font-family:Arial;
  font-weight:bold;
  height: 200px;
  width: 200px;
  border-radius: 4px;
}
img {
  margin-top: none;
  padding-top: none;

}

I was hoping ImgBtn would act as a "constructor" that I could use to invoke the four different buttons, but I got an error. I am pretty sure it all relates to my confusion around how parameters (props?) work in React.

Thank you.

答案1

得分: 2

以下是翻译好的部分:

"The props comes as an object in the components. So you have to destructurine it in your component like this:

function ImgBtn({customImg, btnTxt}) {
    return (
      <>
        <button style = {{backgroundImage : customImg}}>{btnTxt}</button>
      </>   
    );
  }

And in your App.tsx, you pass the props directly in the component like this:

function App() {
  render(
    <div>
      <ImgBtn customImg={practRange} btnTxt={'sample'}/>
    </div>
  );
}

To know more details about how the props works in React, take a look at the official docs.

Hope this answer helped you! :)"

英文:

The props comes as an object in the components. So you have to destructurine it in your component like this:

function ImgBtn({customImg, btnTxt}) {
    return (
     &lt;&gt;
      &lt;button style = {{backgroundImage : customImg}}&gt;{btnTxt}&lt;/button&gt;
     &lt;/&gt;   
      );
  }

And in your App.tsx, you pass the props directly in the component like this:

function App() {
render(
    &lt;div&gt;
     &lt;ImgBtn customImg={practRange} btnTxt={&#39;sample&#39;})/&gt;
   &lt;/div&gt;
);
}

To know more details about how the props works in React, take a look at the official docs.

Hope this answer helped you! 在React中通用化自定义按钮:自定义文本和背景图像

答案2

得分: 1

hm,我认为你的语法会导致错误,它并不完全是React :)。例如, 组件带有这些属性(customImg,btnText),你需要在声明该组件在 App.js 中时传递它们。

function App() {
  return (
    <div>
      <ImgBtn customImg={practRange} btnText="test" />
    </div>
  );
}

也许你会对学习React基础感兴趣。

https://reactjs.org/tutorial/tutorial.html

英文:

hm I think your syntax is causing errors, it's not quite React :). for instance, the <ImgBtn> component comes with these props (customImg, btnText), you will need to pass them when declaring the component in App.js.

function App() {
      return (
        &lt;div&gt;
          &lt;ImgBtn customImg={practRange} btnText=&quot;test&quot; /&gt;
        &lt;/div&gt;
      );
}

perhaps you'd be interested in reading up on React basics.

https://reactjs.org/tutorial/tutorial.html

huangapple
  • 本文由 发表于 2023年2月10日 07:38:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定