组件的使用方法:
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;
这是我的密码. 我目前正在做这件事.
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;
我是编程新手. 我目前正在创建一个有用的open-source NPM Package.
这是它的密码. 一遍又一遍地重复相同的代码.
我希望我能更容易地修改这段代码,但我做不到.
如果你能帮忙,我会很高兴的.
请帮忙,并让我知道你的contribution.
谢谢:)