I want to create visitor counter for my website. I use api to get hit counter. I want to create something like this
I try with code below but the result like this
I want to ask what I need to do to make it like the design I want. what code that I did wrong?
const Card = (value: any) => {
const result = `${value}`.split("");
return (
<div style={{ display: "flex" }}>
{result.map((val) => (
<div
style={{
fontSize: 42,
marginRight: 10,
background: "#8D0000",
color: "white",
width: 40,
height: 55,
fontWeight: "bold",
textAlign: "center"
}}
>
{val}
</div>
))}
</div>
);
};
const Footer = () => {
const [visitor, setvisitor] = useState<visitortype>();
useEffect(() => {
fetch('https://count.cab/hit/ghG6Oirn0b')
.then(response => response.json())
.then(allvisitor=> setvisitor(allvisitor))
.catch(error => console.log(error));
}, []);
return (
<GridItem
w="100%"
h={{ base: "80px", lg: "300px" }}
colSpan={{ base: 8, lg: 4 }}
>
<VStack
align={{ base: "center", lg: "stretch" }}
marginTop={{ base: "0", lg: "60px" }}
marginLeft={{ base: "0", lg: "50px" }}
>
<Card value={visitor?.count}/>
</VStack>
</GridItem>
);
};
I also try using react-flip-number but still not like the style I want
<FlipNumbers
height={25}
width={20}
color="white"
background="#8D0000"
numberStyle={{
fontSize: 16,
}}
play perspective={100}
numbers={`${visitor?.count ?? 0}`}
/>;
2
Answers
Since you are accessing props in card component, you need to modify the result variable like this:
If not, then you need to modify the way you access props and use object destructuring like:
Reflection to your question
The error, I believe, stems from a misunderstanding of how component parameter passing works. You thought that simply declaring the parameter passed to the component is sufficient. It’s not. The component receives an object containing all the passed properties, including the one named "value" in your case. So, if you continue this way, you’ll find it a bit awkward to access the
value
parameter from the object usingvalue.value
.Solution
In React.js, components receive passed variables as an object. You can either declare the object directly (e.g., as
props
) and reference its parameters later, or declare the parameters directly as shown in the second example.More information
Example