Using Javascript Variables with styled-components

前端 未结 3 854
挽巷
挽巷 2020-12-24 13:42

I\'m using styled-components to build my components. All style properties which accept custom values are reused throughout my components (as it should be). With that in mind

3条回答
  •  青春惊慌失措
    2020-12-24 14:41

    Wrapping a around your application may help:

    https://www.styled-components.com/docs/advanced#theming

    const theme = {
      fontColour: 'purple'
    }
    
    render() {
      return (
        
          
        
      )
    }
    

    That will give all child styled-components access to the theme like so:

    const MyApplication = styled.section`
      color: ${props => props.theme.fontColour}
    `
    

    Or

    const MyFancyButton = styled.button`
      background: ${props => props.theme.fontColour}
    `
    

    Or access the theme via https://www.styled-components.com/docs/advanced#getting-the-theme-without-styled-components

提交回复
热议问题