Any image to be embedded inside the <Image/ >
tag
- Add the image file to
src/assets/images
(usinglogo.png
as example) - Add the following lines to
src/assets/images/index.tsx
import logo from "./logo.png";
export const Logo = createImage(logo, "Logo");
- Use the image in any react component like below:
<Logo />
Export tsx components from inside the src/assets/icons
folder using the svgr tool
To add a new font to the project:
- Create and place the fonts inside a
src/fonts/<font-name>
folder - Export a new
localFont
fromsrc/fonts/config.ts
- Import and use the
className
property wherever necessary.
All API calls must be made from within the src/services
folder
- Create a new file for any group of similar services eg:
src/services/example.ts
- Export a new service function like below:
export const exampleService = createFetcher<
ResponseBody,
RequestBody,
RequestUrlParams,
RequestQueryParams,
>({
url: string | (p:RequestUrlParams) => string,
...allAxiosProps
})
- Use the function like below:
const responseBody = await exampleService({
data: requestBody,
params: requestQueryParams,
urlParams: requestUrlParams,
..allAxiosProps // will override the props passed to `createFetcher`
});
useAsyncEffect(effect, deps, destructor);
Variable | Type | Explanation | Required |
---|---|---|---|
effect | () => Promise<void> |
runs whenever contents of deps change |
Yes |
deps | any[] |
triggers effect whenever its content change |
Yes |
desctructor | () => Promise<any> |
runs when the parent component is destoryed | No |
const [value, { set, refresh, fetch, loading }] = useAsyncMemo(effect, deps, {
defaultValue,
storeLocal,
desctructor,
});
Variable | Type | Explanation | Required |
---|---|---|---|
effect | (prevValue:T) => Promise<T> |
An async function whose return value is stored in value variable. It also have the previous value of value |
Yes |
deps | any[] |
any change here triggers effect to run again and update value |
Yes |
defaultValue | T |
value is initialised with this prop and stays the same until the effect() promise resolves |
No |
storeLocal | string |
If passed, will store value to localStorage using the passed string as the key and retain it on page reload |
No |
destructor | () => void |
Runs when the parent component is destroyed | No |
Variable | Type | Explanation |
---|---|---|
value | T |
the state returned by effect |
set | Dispatch<SetStateAction<T>> |
the set state function for the value state |
loading | boolean |
indicated if the async function effect is currently being executed |
refresh | () => void |
triggers effect to run again and update value . Also changes loading to true during the process |
fetch | () => void |
same as refresh but does not change loading state |
import { TextField } from "@/components/FormElements/TextField";
import { Select } from "@/components/FormElements/Select";
import { Switch } from "@/components/FormElements/Switch";
import { useForm } from "@/hooks/useForm";
import * as yup from "yup";
const schema = yup.object().shape({
name: yup.string().required(),
isPresent: yup.boolean(),
});
const FormComponent = () => {
const formProps = useForm({
initialValue: {
name: "",
isPresent: false,
role: "student",
},
schema,
});
const { register } = formProps;
return (
<>
<TextField label="Name" {...register("name")} />
<Switch {...register("isPresent")} />
<Select
options={[
{ value: "student", label: "Student" },
{ value: "teacher", label: "Teacher" },
]}
identifier={(i) => i.item.value} // should return a string
{...register("role")}
/>
</>
);
};
import { FC } from "react";
import { Registered } from "@/hooks/useForm";
import { ErrorMessage } from "@/components/FormElements/ErrorMessage";
interface Props extends Registered<custom_type> {
// more props if needed
}
export const CustomInput: FC<Props> = ({ value, onChange, ...props }) => {
return <div>
{/** Custom UI using value and **/}
<ErrorMessage {...props}>
</div>;
};
import { useForm, FormProvider, useFormContext } from "@/hooks/useForm";
interface FormShape {
// form shape
}
const Parent = () => {
const formProps = useForm<FormShape>({
initialValue: {
// should be `FormShape`
},
});
return (
<FormProvider {...formProps}>
<Child />
<FormProvider>
);
};
const Child = () => {
const fromProps = useFormContext<FormShape>()
return //UI
}
Alternatively, we can use the following Parent
component:
import { Form } from "@/hooks/useForm";
const Parent = () => {
return (
<Form<FormShape>
initialValue={
{
// should be `FormShape`
}
}
>
<Child />
</Form>
);
};