How to convert <img> element to Nextjs <Image> component - reactjs

I've been writing Nextjs for a while but some of the image components are still in <img> tag like this. (based on TailwindCSS)
<div className="relative">
<div className="absolute inset-0 flex flex-col" aria-hidden="true">
<div className="flex-1 bg-transparent" />
<div className="w-full flex-1 bg-black" />
</div>
<div className="mx-auto max-w-7xl px-4 sm:px-6">
<img
className="relative rounded-lg"
src="/images/somephoto.png"
alt="screenshot"
/>
</div>
</div>
The current code shows the image in the center of the page with a relative position.
I've tried to convert the tag into the Next <Image /> component though it is hard to keep the styling since we need to specify width and height.
I would like to know the conversion tips between <img> and Next <Image> if any. (Appreciate it if you have an idea for the above case example)

As your current code is working correctly without adding any additional staying to the img tag, you can just replace it with the <Image /> component from next.js
<div className="relative">
<div className="absolute inset-0 flex flex-col" aria-hidden="true">
<div className="flex-1 bg-transparent" />
<div className="w-full flex-1 bg-black" />
</div>
<div className="mx-auto max-w-7xl px-4 sm:px-6">
<Image
loader={myLoader}
src="me.png"
alt="Picture of the author"
layout='fill'
/>
</div>
</div>;

Related

How to make an animation in React?

I'm trying to figure out how to add an animation on the next elements. I was trying some stuff but didn't figure it out due the fact that I recently started to learn React.
<div className="gradient2">
<div className="grid grid-rows-3 stoljpi-metci grid-flow-col">
<div className="font-bold text-white pl-1.5 bojaTexta"> {metciTren}</div>
<div className="font-bold text-gray-400 -mt-1"> {clipMetci}</div>
</div>
<>
--Anim should go here, I want the className='slikea' to be animated, like slideIn
<div className='slikea'>
<img className="stoljpi mt-1" src={`${process.env.PUBLIC_URL}/images/${state}.png`} />
</div>
</>
</div>

User Image "Display Url" not showing properly at UI when I using firebase for authentication

Note: email-password-based login working properly only happened when login using google.
<li onClick={() => setShow(!show)} className="nav-item cursor-pointer relative">
<div className="flex items-center gap-3">
<img className="w-14 h-14 rounded-full border p-1" src={user?.photoURL} alt="" />
</div>
{show && (
<ul className="absolute top-[70px] left-0 sm:left-auto sm:right-0">
<div className="rounded shadow bg-white " data-bs-popper="none">
<div className="w-[240px] p-5">
<div className="pb-0">
<div className="flex py-1 items-center gap-2">
<img className="rounded-full w-10 h-10" src={loading ? "Loading" : user?.photoURL} alt="profile" />
<div className="flex-fill ms-3 ">
<h4 className="font-bold">{loading ? "Loading" : user?.displayName}</h4>
<small className="">{user?.email}</small>
</div>
</div>
<div>
<div className="border-b-2 my-3" /></div>
</div>
<div className="list-group m-2 flex flex-col gap-3">
<Link to="/my-profile" className="text-lg">
<FontAwesomeIcon className="mr-3" icon={faUser} /> Profile Page
</Link>
<Link to="/my-inventories" className="text-lg">
<FontAwesomeIcon className="mr-3" icon={faFileInvoice} /> My Inventories
</Link>
<Link onClick={() => signOut(auth)} to="/login" className="text-lg">
<FontAwesomeIcon className="mr-3" icon={faSignOut} /> Sign Out
</Link>
</div>
</div>
</div>
</ul>
)}
</li>
see what what happened on UI
Even img get the google user image.
I'm approx. a month late but I think I found the answer
I thinks it's related to the reference policy of the image.
So add referrerpolicy="no-referrer" inside the image.
Hope this fixes your problem.

How do i place the video inside of the React component?

So i have built a react component, and there's supposed to be a video background as a cover, but I just cant seem to make it work.
I just puts the video outside of the component. Can anyone help me with this please. Also I'm using Tailwind.
import React from "react";
export default function Example() {
return (
<div className="items-start flex flex-col p-2 h-96 w-fit ">
<div>
<video autoPlay loop muted>
<source src="Assets/img/video/test.mp4" type="video/mp4" />
</video>
<div className="items-center flex ml-2 mt-2">
<div className="flex-shrink-0 group block">
<div className="flex items-center">
<div>
<img
className="inline-block h-9 w-9 rounded-full"
src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80"
alt=""
/>
</div>
<div className="ml-3">
<p className="text-sm font-medium text-white">Sermad Alladin</p>
<p className="text-sm font-medium text-gray-50">
Elgiganten • 18 t.
</p>
</div>
</div>
</div>
</div>
<div className="items-end self-end flex mt-64">
<div className="flex">
<p className="font-normal h-12 leading-5 w-40 mr-2">
Amazing customer service, but i wish was better...😉
</p>
</div>
<div className="items-center flex h-7 justify-end mb-1 p-2 bg-primary rounded-tl-xl rounded-bl-xl">
<div className="font-bold mx-2 text-white">87</div>
</div>
</div>
</div>
</div>
);
}

Alignment with Tailwind and React

I'm trying to get two different visualizations to properly sit next to each other.
This is the current styling...
<div className="flex flex-col md:flex-row" >
<div className="w-full md:w-2/3">
<div className="text-center mt-4 flex justify-center items-center">
<h1 className="text-blue-500 text-lg font-semibold mr-5">
{format(new Date(selectedDate), 'do, MMMM - yyyy')}{' '}
</h1>
{isLoading && <Loader />}
</div>
<div>
<div className = "grid-cols-2 grid-flow-col" >
<div className = "grid-cols-1">
<h3 className="capitalize">Moodmap Daily Index</h3>
<Overview data={lineGraphData} timing={settingsData} />
</div>
<div>
<Expressions data={expressionsData} />
</div>
</div>
</div>
<div style={{ height: 100, width: 1000 }}>
<SubstanceDetails
fetchSubstances={fetchSubstances}
loading={loading}
data={data}
/>
</div>
</div>
</div>
For some reason when it renders, although I've made a bunch of changes, it comes out like this,
I want them to sit horizontally, with the bottom graph on the right hand side, ideally with the line graph a lot larger, taking up say 2/3. Unsure why this bug keeps happening.
You need to create a grid container by including the grid className in the element where you specify your grid columns:
<div className = "grid-cols-2 grid-flow-col" >
should become:
<div className = "grid grid-cols-2 grid-flow-col" >
https://tailwindcss.com/docs/display#grid
Note, to debug this you could have used the inspector to see that there was no grid container in your output.

Error: Unable to resolve image URL from source (null)

I am using Sanity and React to create a blog page. Everything seems to be okay but when you click on a blog to read more I get the error message "Error: Unable to resolve image URL from source (null)" What could be the issue? Below is part of my OnePage.js page where console shows there's an error
</h2>
<div className="flex justify-center text-gray-800">
<img
src={urlFor(postData.authorImage).url()}
className="w-10 h-10 rounded-full"
alt=""
/>
It looks like authorImage in this case is null, meaning the author doesn't have an image. To prevent this from happening, you can check whether or not the author has an image before rendering the image tag. In other words, something like this:
</h2>
<div className="flex justify-center text-gray-800">
{postData.authorImage && (
<img
src={urlFor(postData.authorImage).url()}
className="w-10 h-10 rounded-full"
alt=""
/>
)}
Instead of passing it before the image element, you can pass the operator in the inside the src`
<div className="flex justify-center text-gray-800">
<img
src={urlFor(postData.authorImage && postData.authorImage ).url()}
className="w-10 h-10 rounded-full"
alt=""
/>
</div>
`

Resources