简体   繁体   English

更新传入的组件的样式-ReactJS

[英]Updating the Styling of a component passed in - ReactJS

I am fairly new to React and I am stuck in styling a component using styled components. 我对React还是很陌生,我被困在使用样式化组件对组件进行样式设置中。

const SearchBarContainer_Button = styled.button`
      padding: 10px;
      margin-left: 10px;
      width: 300px;
`;


<ButtonOne
  style={SearchBarContainer_Button}
  type="submit"
  className="search-bar__button"
>
  {this.props.buttonText || 'search'}
</ButtonOne>

Here is what I have in my ButtonOne: 这是我ButtonOne的功能:

import React from 'react';
import styled from 'styled-components';

const Button_One = styled.button`
      cursor:pointer;
      border: none;
      background-color: #fff;
      color: #000;
      font-size: 12px;
      font-weight: 900;
`;

export const ButtonOne = (props) => (
  <Button_One className={props.className}>{props.children}</Button_One>
);

I have no idea what I am doing wrong, and I would really appreciate some guidance. 我不知道自己在做什么错,我非常感谢您提供一些指导。

Thank you. 谢谢。

Hmm did you read the documentation for styled components? 嗯,您阅读过样式化组件的文档吗? they are styled COMPONENTS 他们被称为组件

which means when you specify 这意味着当您指定

const Button_One = styled.button`
 cursor: pointer
`

you need to use it as component 您需要将其用作组件

<Button_one ...props />

You can't pass style={StyledComponent} it's component not object literal with style. 您不能传递style={StyledComponent}它的组件不是带有样式的对象文字。 IF you want to extend the styled components here is the link https://www.styled-components.com/docs/basics#extending-styles 如果要扩展样式化的组件,请在此处链接https://www.styled-components.com/docs/basics#extending-styles

When used styled-components , you're creating a new component with a certain styles. 使用styled-components时 ,您将创建具有特定样式的新组件。 For your search bar, you're creating a component named SearchBarContainer_Button as that is your variable name. 对于您的搜索栏,您正在创建一个名为SearchBarContainer_Button的组件,因为这是您的变量名。 You do not need to add the style again. 您无需再次添加样式。 Try reviewing the documentation, or reference this example; 尝试查看文档,或参考此示例;

const Title = styled.h1`
  font-size: 1.5em;
  text-align: center;
  color: red;
`;

And to use your component Title with the specified styles: 并以指定的样式使用组件Title

<Title>Content of component</Title>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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