英文:
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 './App.css';
function ImgBtn(customImg, btnTxt) {
return (
<>
<button style = {{backgroundImage : customImg}}>'btnTxt'</button>
</>
);
}
export default ImgBtn;
**App.js **
(Where I would invoke the four instances of ImgBtn, just focusing on one for now)
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 **
(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 (
<>
<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!
答案2
得分: 1
hm,我认为你的语法会导致错误,它并不完全是React :)。例如,
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 (
<div>
<ImgBtn customImg={practRange} btnText="test" />
</div>
);
}
perhaps you'd be interested in reading up on React basics.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论