What is the best way to transmit the "theme" property to "Components" within a next.js application?

I'm currently working on a next.js app and dealing with the _app.tsx file:

import '../styles/globals.css'
import type { AppProps } from 'next/app'
import { createTheme } from '@arwes/design'
import { ThemeProvider, Global, css } from '@emotion/react'
import { globalStyles, blueOnBlack } from '../shared/styles.js'

function MyApp({ Component, pageProps }: AppProps) {
  const theme = createTheme();
  return (
    <ThemeProvider theme={theme}>
      {globalStyles}
      <div style={blueOnBlack(theme)}>
        Futuristic Sci-Fi UI Web Framework
      </div>

      <Component {...pageProps} />
    </ThemeProvider>
  )
}

export default MyApp

An issue I'm facing is needing to access the theme in index.tsx. How can I effectively pass this information into Component?

Answer №1

When working with child components, remember to include:

const style =  useContext(StyleProvider)

Answer №2

By utilizing emotion's useTheme(), I successfully accessed the parent's theme. It was necessary to construct the component outside of the render section within the page's exported function, as illustrated in the index.tsx file:

function CustomText(props: any) {
  const theme = useTheme()
  return <div style={{ color: theme.palette.primary.dark3 }} {...props} />
}


// The defined "CustomText" function can now be utilized as a component in the "Home" component


const Home: NextPage = () => {
    return (
   ...
    <CustomText className={styles.description}>
      Get started by editing{' '}
      <code className={styles.code}>pages/index.tsx</code>
    </CustomText>
   ...
  )  
}

export default Home

This setup functions effectively due to the color being specified as "dark3" in the text above.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The 'SVGResize' or 'onresize' property is not available on the 'SVGProps<SVGSVGElement>' type

Using React with SVG I'm facing an issue with handling the resizing event of an svg element. I have looked into using the SVGResize and onresize events, but encountered compilation errors when trying to implement them: const msg1 = (e: any) => co ...

Differences in Loading Gif Visualization Across Chrome, Safari, and Firefox

Having an issue with a loading image in Chrome, while it displays correctly in Safari and Firefox. In Chrome, I see two half-loading images instead of one whole image. Struggling to find a solution for this problem, any assistance would be greatly apprecia ...

It seems that the `to` required prop was missing in the `Link` component of React-Router

Currently, I am facing an issue while trying to integrate react-router. The error message I'm encountering is: Failed propType: Required prop to was not specified in Link. Check the render method of app. Unfortunately, I am unable to pinpoint ex ...

Establishing Redux States within the Provider (error: Provider encountering useMemo issue)

Exploring redux for state management has been a new journey for me. I am hoping it will help reduce API calls and increase speed, but I've hit a roadblock with an error that I can't seem to figure out. To troubleshoot, I created a simplified vers ...

Issue with Bootstrap v3.3.6 Dropdown Functionality

previewCan someone help me figure out why my Bootstrap dropdown menu is not working correctly? I recently downloaded Bootstrap to create a custom design, and while the carousel is functioning properly, when I click on the dropdown button, the dropdown-menu ...

Is there a way to execute an Apollo GraphQL query prior to the initial rendering of the App component? (while also relying on it)

How can I effectively call an Apollo query in my App component, where the component's behavior depends on the result? My goal is to redirect users to different pages based on their privileges. I'm struggling with figuring out the right way an ...

Discover the power of using the Apollo useMutation function within a customized React hook

I'm struggling to understand Hooks, particularly when I encounter an Invalid hook call error. The latest issue is with using the useMutation hook from Apollo within a custom hook. Can someone please help me figure out what's going wrong? Compone ...

Issue with Create React App 4: Custom eslint configuration not being implemented properly while compiling

Explanation In the past, it was necessary to enable the EXTEND_ESLINT flag in the .env file and include it in the package.json // package.json "eslintConfig": { "extends": [ "react-app", "./.eslintrc.jso ...

Deployment of the Next.js project on Vercel was unsuccessful, with no specific information provided and no build logs

After attempting to deploy a new NextJs project on Vercel, I have encountered an issue with the deployment process. Unfortunately, there seems to be no specific error message or build logs indicating why the deployment failed. The only message I receive i ...

Adjusting Images and Icons' Sizes Automatically on a Single Line

Imagine a row of social media icons/images (like Facebook, Twitter, LinkedIn) displayed together. How can I ensure that when I add more icons/images to this row, their sizes adjust automatically to stay on the same line instead of moving to the next one? I ...

You can only use a parameter initializer within the implementation of a function or constructor

I recently started learning TypeScript and am currently using it for React Bricks. I've been working on rendering a 3D object with three.js, but I keep encountering the error mentioned above. I've attempted various solutions such as passing color ...

Adjust the size of multiple elements upon hovering over one of them

I encountered a puzzling issue, and here is a demonstration of my problem: https://jsfiddle.net/k1y8afst/ <div class="Sample"> <div class="Dummy"> <div class="Books"> <a st ...

Encountering an issue while attempting to connect to localhost:56391/ws using ASP.Net Core WEB API with ReactJS - the connection failed due to an error in the establishment process: net

Description: I have created an ASP.NET WEB API integrated with ReactJS. However, when I try to run it, I encounter ERR_SSL_PROTOCOL_ERROR. I am unsure of what the issue could be. Steps Taken: Created a project in ASP.NET CORE WEB API (6.0) Created a fold ...

Navigating through a diagonal path

Struggling to craft a diagonal navigation system similar to the images below. Despite its seemingly simplicity, I'm stuck investing too much time in figuring it out. My goal is for the menu container to smoothly reveal from right to left when clicking ...

What is the reason behind the cross-origin error thrown by JSON.parse?

When I don't use JSON.parse, my code works perfectly fine. However, as soon as I attempt to parse or stringify my data object, a cross-origin error is thrown. Why is this occurring and what steps can I take to resolve it? The snippet of code in Title ...

What is the best way to expand the navigation bar: should I add a side div or incorporate a background image?

I am encountering two issues that I need help solving. My first problem involves stretching out the navigation bar to fill the height and length of the adjacent div (div#textarea) while also keeping the text of the menu centered within that div. The seco ...

"Trouble with props: List items not showing up after refreshing the page

I am facing an issue with my "Event Selector" component where it is not displaying the list items as expected. The component is supposed to create a button for each item in the 'lists' passed via props. Strangely, the items do not show up upon re ...

Your current Node version is 12.13.0, but in order to use Create React App, you need to have Node 14 or higher installed. Kindly

While embarking on a new React project using create-react-app, I encountered an error message stating "You are running Node 12.13.0. Create React App requires Node 14 or higher. Please update your version of Node." Despite attempting various solutions sug ...

Designing a card flip animation featuring front and back faces without using absolute positioning

Looking to enhance my website with a new feature that incorporates the card-flipper schema found on David Walsh's site: https://davidwalsh.name/css-flip. The challenge arises from the fact that the content within my cards is dynamic, making the height ...

What is the best way to present sorted items on a user interface?

I have a unique Med interface containing properties like drugClass, dosage, and name. Using this interface, I created an array of drugs with different attributes. How can I filter this array by drugClass and then display the filtered data on a web page? ...