1.1.0 • Published 3 years ago

@ithreat/use-function v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@ithreat/with-function

Made with create-react-library

NPM JavaScript Style Guide

Install

npm install --save @ithreat/with-function

Usage

import React, { useContext } from 'react'
import {useFunction} from '@ithreat/use-function';
import {useResults} from '@ithreat/use-results';

const App = () => {
  const {db, stitchClient} = useContext(StitchContext);
  const {setToast} = useContext(ToastContext);
  const query = {};
  const code = 'company-name';
  const results = useResults({
      defaults: {
          pageSize: 25,
          page: 1,
          sortBy: { field: 'field-name', order: -1 },
      },
      watch: { collection: db.collection('collection-name') },
      hook: useFunction({
          func: 'function-name',
          args: [ code, query ],
          onResponse: (res) => {
              results.setCount(res.count);
              results.setResults(res.results.map((r) => ({...r, id: r._id.toString()})));
          },
          setToast,
          stitchClient,
      }),
      onChangePage: ({hook, page, pageSize, sortBy}) => {
          hook.execute([hook.args[0], hook.args[1], (page - 1) * pageSize, pageSize, sortBy.field, sortBy.order]);
      },
  });

  return (
    <div>
      Hook for running a mongo function.  Works well with @ithreat/use-results hook.
    </div>
  )

};

export default App;

License

MIT © djm-ithreat