I used "npx create-next-app" to create my NextJs project, and now encountered an interaction with _app.js to describe TransactionContext, but the problem is that it is not in my project
I would be grateful for help
Instead of the pages folder, I only have layout.tsx and page.tsx
Be careful, you created your project using the app folder. It is a new way for next.js to work, but it is in beta and for now not available for production because of some bugs.
Try to recreate your project using standard src folder. From what i remember, when you do a create-next-app, the terminal ask your preferences.
In Next.js, the _app.js file is no longer used starting from Next.js version 9.3. Instead, you should use the pages/_app.js file to customize the default Next.js App component.
In Next.js, the pages/_app.js file is responsible for initializing pages, rendering the main App component, and wrapping it with other components that should be present on every page. It allows you to override the default App component provided by Next.js and add global styles, state management, and other customizations.
Here’s an example of how you can use the pages/_app.js file:
// pages/_app.js
import React from ‘react’;
import App from ‘next/app’;
function MyApp({ Component, pageProps }) {
// Add any custom logic or components here
return <Component {…pageProps} />;
}
export default MyApp;
In this example, MyApp is the custom App component. You can add any necessary global components, context providers, or custom logic within the MyApp component.
Note that if you’re using Next.js version 12 or later, the file name should be _app.tsx or _app.jsx depending on whether you’re using TypeScript or JavaScript, respectively.
Make sure to restart your Next.js development server after creating or modifying the _app.js file for the changes to take effect.
2
Answers
Be careful, you created your project using the app folder. It is a new way for next.js to work, but it is in beta and for now not available for production because of some bugs.
Try to recreate your project using standard src folder. From what i remember, when you do a create-next-app, the terminal ask your preferences.
See below :
https://nextjs.org/docs/getting-started/installation
In Next.js, the
_app.js
file is no longer used starting from Next.js version 9.3. Instead, you should use thepages/_app.js
file to customize the default Next.jsApp
component.In Next.js, the
pages/_app.js
file is responsible for initializing pages, rendering the mainApp
component, and wrapping it with other components that should be present on every page. It allows you to override the defaultApp
component provided by Next.js and add global styles, state management, and other customizations.Here’s an example of how you can use the
pages/_app.js
file:// pages/_app.js
import React from ‘react’;
import App from ‘next/app’;
function MyApp({ Component, pageProps }) {
// Add any custom logic or components here
return <Component {…pageProps} />;
}
export default MyApp;
In this example,
MyApp
is the customApp
component. You can add any necessary global components, context providers, or custom logic within theMyApp
component.Note that if you’re using Next.js version 12 or later, the file name should be
_app.tsx
or_app.jsx
depending on whether you’re using TypeScript or JavaScript, respectively.Make sure to restart your Next.js development server after creating or modifying the
_app.js
file for the changes to take effect.