Using props in React with styled components across components

Roy

I have a React component:

const StyledButton = styled.button`
    border: none;
    background: ${props => props.color};
    color: white;
    &:focus {
        outline: none;
    }
`;

const Button = (props) => {
    return (
        <StyledButton>{props.name}</StyledButton>
    )
};

and another component:

const NoteWindow = (props) => {
    return (
        <StyledNoteWindow className="NoteWindow">
            <StyledForm action="#" className="form">
                <StyledInput type="text" name="title" id="title" autoComplete="off" placeholder="Title" style={{fontSize: 60, fontWeight: 700}}/>
                <StyledInput type="text" name="content" id="content" autoComplete="off" placeholder="Content.." style={{fontSize: 20}}/>
            </StyledForm>
            <Button name="Discard" color="red"/>
            <Button name="Save" color="blue"/>
        </StyledNoteWindow>
    )
}

and I want the background of my button component to be the "color" attribute in the other element. How can I do that? (Everything is imported and exported)

wentjun

From what I can see, Button and StyledButton components accepts at least the following props, name and color.

Therefore, you will need to pass the props down to the StyledButton component:

const Button = ({ color, name }) => (
  <StyledButton color={color}>{name}</StyledButton>
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using props inside react styled components

Using styled components with props and typescript

Using styled-components with props from React component

React Styled-Components passing props issue

Passing Props to React Styled Components in Typescript

How to use this.props in React Styled Components

React — Passing props with styled-components

Using ThemeProvider props in Global Styled-Components

Set Background Image using props in Styled Components

Styled Components: props for hover

Send multiple props across components React

Styling react-router-dom Link using styled-components getting warning when passing props

Mapping Through Props in Styled Components

Multiple Props Options for Styled Components

Responsive Props In ReactJS Styled Components

how to pass props to styled components

Styled-components extends props

Using CSS attr() with styled components in react Hooks

How to style react styled components using classNames

Using styled-components to style my own React components

On styling of class components using React styled-components

Prevent React from displaying props meant for Styled-components?

How to get props from parent in React Styled Components

Styled-components in React.js - props doesn't work

React + Typescript + Styled Components pass props to keyframes & conditioning

Setup for React styled components

Styled Components rendering Styled tag based on props

Dynamically Styled Button in React Native using Styled Components

Using Styled Components and passing props to psedo element before not working

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive