m-model-react
TypeScript icon, indicating that this package has built-in type declarations

0.0.8 • Public • Published

m-model-react

m-model-react is designed to be used with m-model-core in the world of react. It provides several useful optimized hooks

For the rest of the docuemnt, assume that we have already created model with m-model-core and called it User

Hooks

useModelDocById

import React from "react";
import { useModelDocById } from "m-model-react";
const UserShortInfoComponent = ({ userId }: { userId: number }) => {
    const user = useModelDocById(User, userId);
    if (!user) {
        return <div>loading...</div>;
    }
    return (
        <div>
            {user.getFullname()}
        </div>
    );
};

useModelDocByQuery

import React, { useMemo }  from "react";
import { useModelDocById } from "m-model-react";
const UserShortInfoComponent = ({ firstname }: { firstname?: string }) => {
    const query = useMemo(() => {
        if (!firstname) return null;
        return { firstname }
    }, [firstname]);
    
    const user = useModelDocByQuery(User, query);
    if (!user) {
        return <div>loading...</div>;
    }
    return (
        <div>
            {user.getFullname()}
        </div>
    );
};

useModelDocsByIds

Similar to useModelDocById, just pass memoized array of ids and it will return array of model instances

useModelDocByQuery

Similar to useModelDocById. It will return array of model instances

useModelDocs

returns array of all instances of model

Readme

Keywords

none

Package Sidebar

Install

npm i m-model-react

Weekly Downloads

4

Version

0.0.8

License

MIT

Unpacked Size

8.27 kB

Total Files

6

Last publish

Collaborators

  • thomasmikava