way to use component:
import React from "react";
import MyAvatars from "../../components/MyAvatar/MyAvatars";
const About = () => {
return (
<MyAvatars
topType={[true, 1000]}
accessoriesType={[true, 1200]}
facialHairType={["blank"]}
/>
);
};
export default About;
This is my code. I am currently working on this.
Code:
import React from "react";
import Avatar from "avataaars";
import { Top, Accessories, FacialHair } from "./avatarThings";
const MyAvatar = ({ topType, facialHairType }) => {
const [avatar, setAvatar] = React.useState({
avatarStyle: "Circle",
topType: "ShortHairDreads01",
accessoriesType: "Blank",
facialHairType: "Blank",
});
const topFunc = () =>{
if (topType.length === 2) {
const interval = setInterval(() => {
setAvatar((prev) => {
return {
...prev,
topType: Top[Math.floor(Math.random() * Top.length)],
};
});
}, topType[1]); //this is time
return () => clearInterval(interval);
}
setAvatar((prev) => {
return {
...prev,
topType: topType[0],
};
});
}
const FacialHairFunc = () =>{
if (facialHairType.length === 2) {
const interval = setInterval(() => {
setAvatar((prev) => {
return {
...prev,
facialHairType: FacialHair[Math.floor(Math.random() * FacialHair.length)],
};
});
}, facialHairType[1]);
return () => clearInterval(interval);
}
setAvatar((prev) => {
return {
...prev,
facialHairType: facialHairType[0],
};
});
}
React.useEffect(() => {
if (topType) {
topFunc()
}
if (facialHairType) {
FacialHairFunc()
}
}, [])
return (
<Avatar
avatarStyle="Circle"
topType={avatar.topType}
accessoriesType={avatar.accessoriesType}
hairColor={avatar.hairColor}
facialHairType={avatar.facialHairType}
/>
);
};
export default MyAvatar;
I am new to programming. I am currently creating a useful open-source NPM Package.
This is the code for it. Repeat the same code over and over again.
I wish I could modify this code more easily than this, but I could not. It's very easy for people to admire you. And I'll learn new things with the code you write for this.
I would be very glad if you help.
Please help and let me know your contribution as well.
Thanks :)
