REECT本地:带有颜色参数的自定义组件

发布于 2025-01-25 12:09:30 字数 743 浏览 1 评论 0原文

我正在进行练习,以学习在编码上学习反应的人。

我被告知“在React中,属性作为对象作为第一个参数的对象传递给我们的组件。您需要在自定义组件中添加此参数,并将颜色属性用作背景颜色。”

我需要将颜色作为参数传递给我的盒子自定义组件。这是我的代码:

export const Box = (color) => (
<View color={color} style={{ width: 100, height: 100, backgroundColor: this.props.color }} />
  );

它给我带来了语法错误。 我还尝试了:

export const Box = (color) => (
<View style={{ width: 100, height: 100, backgroundColor: color }} />
  );

但是我被告知“视图应该具有颜色属性设置的背景颜色。”。当我这样做时,这是一样的

 export const Box = (color) => (
    <View style={{ width: 100, height: 100, backgroundColor: {color} }} />
      );

,但是在调用react和正确使用它们的变量时,我总是误会了……

如果您能帮助我,那就太好了!

谢谢

Im doing the exercises to learn react native on codecademy.

I am told "In React, properties are passed as objects on the first parameter to our components. You need to add this parameter in the custom component and use the color property as the background color."

I need to pass color as a parameter to my Box custom component. This is my code:

export const Box = (color) => (
<View color={color} style={{ width: 100, height: 100, backgroundColor: this.props.color }} />
  );

It throws me a syntax error.
I also tried :

export const Box = (color) => (
<View style={{ width: 100, height: 100, backgroundColor: color }} />
  );

But I am told "View should have a background color set by the color property.". It's the same when I do

 export const Box = (color) => (
    <View style={{ width: 100, height: 100, backgroundColor: {color} }} />
      );

It's very basic but I'm always mistaken when it comes to calling variables in React and properly using them...

If you could help me that would be great!

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2025-02-01 12:09:30

基本上,您可以得到这样的道具:

export const Box = ( props ) => (
<View color={props.color} style={{ width: 100, height: 100, backgroundColor: props.color }} />);

Basically, you can get props like this:

export const Box = ( props ) => (
<View color={props.color} style={{ width: 100, height: 100, backgroundColor: props.color }} />);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文