Skip to content

How do I access theme when using css-backticks notation? #2717

Answered by carlos-dubon
pauljfx asked this question in Q&A
Discussion options

You must be logged in to vote

@pauljfx

This is as clean as I could get it:

MyComponent.jsx

/** @jsx jsx */
import { jsx, useTheme, css } from '@emotion/react';

const MyComponent = () => {

  const theme = useTheme();  

  const myStyles = css`
    color: ${theme.colors.primary}
  `;

  return(
    <>
      <div css={myStyles}>I am a div tag</div>
      <p css={myStyles}>I am a paragraph tag</p>
      <h2 css={css`color: ${theme.colors.primary}`}>I am a Heading 2 tag</h2>
    </>
  )
}

export default MyComponent;

In react there is a point where you need to have a parent component, in this case it's the MyComponent component.

Hooks can only be called before the return statement of a Functional Component. So it´s impos…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
3 replies
@pauljfx
Comment options

@carlos-dubon
Comment options

@pauljfx
Comment options

Comment options

You must be logged in to vote
2 replies
@pauljfx
Comment options

@pauljfx
Comment options

Answer selected by pauljfx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants