28
loading...
This website collects cookies to deliver better user experience
$ mkdir next-food-app
$ cd next-food-app
$ npm init -y
$ npm install react react-dom
$ npm install next
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
Note: Next.js comes with the file-system based routing feature, which makes all files added to the “/pages” directory available as routes. All files except “index.js”, which the Router automatically routes to the root of the directory.
/pages/index.js
=> /
/pages/meals/index.js
=> /meals
/pages/meals.js
=> /meals
const HomePage = () => {
return <h1>Welcome to our Food Next.js App 😊</h1>;
};
export default HomePage;
$ npm run dev
$ npm install tailwindcss postcss autoprefixer
$ npx tailwindcss init -p
module.exports = {
purge: ["./pages/**/*.{js,ts,jsx,tsx}", "./components/**/*.{js,ts,jsx,tsx}"],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
};
@tailwind base;
@tailwind components;
@tailwind utilities;
Note: In Next.js the file "_app.js" in the "/pages" directory overrides the default “App” component that allows us to add global css to the whole application.
import "../public/style/global.css";
const MyApp = ({ Component, pageProps }) => {
return <Component {...pageProps} />;
};
export default MyApp;
$ npm run dev
and open your browser on http://localhost:3000 .Note: Next.js is built on top on react, therefore react components are part of it.
const MealItem = () => {
return (
<div className="w-1/4 p-8 shadow-lg rounded-lg bg-yellow-50">
<img src="/img/meal1.PNG" className="w-auto h-auto" alt="Chicken Salad" />
<div className="text-center py-2">
<h3 className="text-xl font-normal">
Chicken Salad
<span className="px-3 font-light text-yellow-500">(4 dishes)</span>
</h3>
<p className="text-gray-500 text-base">Herico de Porto</p>
<button className="bg-yellow-500 px-4 py-2 rounded-lg text-gray-50 font-medium mt-2">
Details
</button>
</div>
</div>
);
};
export default MealItem;
import MealItem from "./MealItem";
const MealList = () => {
return (
<div className="flex flex-wrap">
<MealItem />
</div>
);
};
export default MealList;
import MealList from "../components/MealList";
const HomePage = () => {
return <MealList />;
};
export default HomePage;
Download the image found on this link -> https://github.com/musole-masu/nextjs-food-app/blob/main/public/img/meal1.PNG
Create in the "/public" directory a folder named "/img" and put the downloaded image in there.
Now, wrap your pages with the style added in the "_app.js" just like it's done below:
import "../public/style/global.css";
const MyApp = ({ Component, pageProps }) => {
return (
<div className="max-w-7xl mx-auto py-40">
<Component {...pageProps} />
</div>
);
};
export default MyApp;
$ npm run dev
, then go to http://localhost:300