How to use swr - 10 common examples

To help you get started, we’ve selected a few swr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zeit / swr / examples / optimistic-ui / pages / index.js View on Github external
async function handleSubmit(event) {
    event.preventDefault()
    // mutate current data to optimistically update the UI
    // the fetch below could fail, in that case the UI will
    // be in an incorrect state
    mutate('/api/data', [...data, text], false)
    // send text to the API
    await fetch('/api/data', {
      method: 'POST',
      body: JSON.stringify({ text })
    })
    // to revalidate the data and ensure is not incorrect
    // we trigger a revalidation of the data
    trigger('/api/data')
    setText('')
  }
github zeit / swr / examples / optimistic-ui / pages / index.js View on Github external
async function handleSubmit(event) {
    event.preventDefault()
    // mutate current data to optimistically update the UI
    // the fetch below could fail, in that case the UI will
    // be in an incorrect state
    mutate('/api/data', [...data, text], false)
    // send text to the API
    await fetch('/api/data', {
      method: 'POST',
      body: JSON.stringify({ text })
    })
    // to revalidate the data and ensure is not incorrect
    // we trigger a revalidation of the data
    trigger('/api/data')
    setText('')
  }
github zeit / swr / examples / pagination / pages / index.js View on Github external
export default () => {
  const {
    pages,
    isLoadingMore,
    isReachingEnd,
    loadMore
  } = useSWRPages(
    // page key
    'demo-page',

    // page component
    ({ offset, withSWR }) => {
      const { data: projects } = withSWR(
        // use the wrapper to wrap the *pagination API SWR*
        useSWR('/api/projects?offset=' + (offset || 0), fetch)
      )
      // you can still use other SWRs outside

      if (!projects) {
        return <p>loading</p>
      }

      return projects.map(project =&gt;
github zeit / swr / examples / api-hooks / hooks / use-projects.js View on Github external
function useProjects() {
  return useSWR('/api/data', fetch)
}
github ejarzo / Shape-Your-Music / src / middleware / withData.js View on Github external
export const withData = (key, getData) =&gt; Component =&gt; props =&gt; {
  const { data: result, error } = useSWR(key, getData, {
    onError: captureException,
  });

  if (!!error) {
    return <div style="{{">Error: {error.message}</div>;
  }

  const loading = !result;
  if (loading) {
    return ;
  }

  return (
github openmultiplayer / web / frontend / src / pages / servers / [ip].tsx View on Github external
const Page = ({ ip, initialData }: Props) =&gt; {
  const { data, error } = useSWR(ip, getServer, {
    initialData,
  });

  return (
    &lt;&gt;
      <section>
        
          <a>Back</a>
        

        {error ?  : }
      </section>
    
  );
};
github zeit / swr / examples / basic / pages / index.js View on Github external
export default () =&gt; {
  const { data } = useSWR('/api/data', fetch)

  return <div style="{{">
    <h1>Trending Projects</h1>
    <div>
    {
      data ? data.map(project =&gt; 
        <p><a>{project}</a></p>
      ) : 'loading...'
    }
    </div>
  </div>
}
github umijs / umi / packages / umi-plugin-ui / src / plugins / dashboard / ui / plugins / dailyReportHeader.tsx View on Github external
const DailyReportHeader: React.SFC = props =&gt; {
  const { api } = React.useContext(Context);
  const { _, intl } = api;
  const { data: list } = useSWR('zaobao.list', async () =&gt; {
    const { data } = await api.callRemote({
      type: 'org.umi.dashboard.zaobao.list',
    });
    return data;
  });

  return (
    Array.isArray(list) &amp;&amp; (
      <select>
        {(list || []).map(item =&gt; (
          
            {moment(item.createdAt).format('YYYY-MM-DD')}
          
        ))}
      </select>
    )
github sorrycc / learn-react-with-umi / swr / layouts / index.js View on Github external
export default function (props) {
  const { data, revalidate } = useSWR('/api/login', fetch);
  if (!data) return <h1>loading...</h1>;
  if (data.loggedIn) {
    return (
      <div>
        <h1>Welcome, { data.name }</h1>
        <img height="{80}" width="{80}" src="{data.avatar}">
        <button> {
          logout();
          revalidate();
        }}&gt;Logout</button>
        { props.children }
      </div>
    );
  } else {
    return (
      <div></div>
github zeit / swr / examples / basic-typescript / pages / index.tsx View on Github external
export default () =&gt; {
  const { data } = useSWR('/api/data', fetch)

  return (
    <div style="{{">
      <h1>Trending Projects</h1>
      <div>
        {data
          ? data.map(project =&gt; (
              <p>
                
                  <a>{project}</a>
                
              </p>
            ))
          : 'loading...'}
      </div>
    </div>

swr

React Hooks library for remote data fetching

MIT
Latest version published 10 months ago

Package Health Score

95 / 100
Full package analysis