Easily automate your version and build numbers incrementally in Xcode and use git hooks to ensure version consistency. This is a great way to ensure that your versioning is consistent across all of your projects without all the manual hassles.
I use a lot of color in my projects. If you're looking for a simple function, don't install a package from NPM. Instead, first try to understand the problem, solution and of course the code before you reach for a new module.
A random color generator is a handy tool to have in your toolbox. Here's a simple function that returns a random color in TypeScript for use in React, Vue, Angular, or any other JavaScript project.
export const randomColor = () => {
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
return "#" + randomColor;
};
class ColorError extends Error {
constructor(message?: string) {
super(message);
this.name = "ColorError";
}
}
export const randomColor = (): string => {
const randomColor: number = Math.floor(Math.random() * 16777215);
const colorString: string = + randomColor.();
(!.(colorString)) {
();
}
colorString;
};
import { useState, useEffect } from "react";
const useRandomColor = () => {
const [color, setColor] = useState<string>("#FFFFFF");
useEffect(() => {
try {
setColor(randomColor());
} catch (error) {
if (error instanceof ColorError) {
console.error(error.message);
} else {
throw error;
}
}
}, []);
return color;
};
export default useRandomColor;
import React from "react";
import useRandomColor from "./useRandomColor";
const ColorfulComponent: React.FC = () => {
const color = useRandomColor();
return <div style={{ backgroundColor: color }}>Hello, world!</div>;
};
export default ColorfulComponent;
Hope you enjoyed reading my blog and learning how to use this simople random color function in TypeScript. If you have any questions, please feel free to reach out to me on Twitter or LinkedIn.
Please consider Buying Me A Coffee. I work hard to bring you my best content and any support would be greatly appreciated. Thank you for your support!