Giter Club home page Giter Club logo

react-directus's Introduction

Directus logo

A set of React components and utilities for Directus Headless CMS.

NPM version NPM downloads Travis build GitHub issues dependencies status devDependencies status

🚀 Quick start

Install this library along with @directus/sdk:

npm install react-directus @directus/sdk

The <DirectusProvider> component makes the Directus SDK available to any nested components that need to access it. Assuming that the <App /> component is the root-level component:

import { App } from './App';
import { DirectusProvider } from 'react-directus';
import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
  <DirectusProvider apiUrl="https://api.example.com">
    <App />
  </DirectusProvider>,
  document.getElementById('root')
);

You can optionally pass an apiOptions object to the provider, it will be passed to the client options.

⚙️ The hook useDirectus

After adding the provider, you can access the configured client anywhere in the app, using the useDirectus hook:

import React, { useEffect, useState } from 'react';
import { useDirectus } from 'directus-react'

export const TodoList = () => {
  // Get the Directus SDK object
  const { directus } = useDirectus();
  const [todos, setTodos] = useState([]);

  useEffect(() => {
    const fetchTodos = async () => {
      const todos = (await directus.items('todos').readMany()).data;
      setTodos(todos);
    };

    fetchTodos();
  }, [directus]);

  return todos.map(item => <TodoItem key={item.id} item={item} />);
};

Components

The hook exports also the <DirectusAsset> and <DirectusImage> components, for easy access to API assets. They are configured for using the apiUrl specified in the provider.

These components, when imported from react-directus directly (i.e. not using the hook), can be used in a "standalone" way, meaning that they are not bound to the apiUrl specified in the provider. In that case, they both accept an apiUrl prop.

<DirectusAsset>

Computes the URL of the given resource asset, rendering it using the render prop:

  • asset: the asset representing the resource (string or object with an id property)
  • render: a function (which receive an object with the url property) that should return the component to render
  • download: force browser to download the asset (force the Content-Disposition header)
import React from 'react';
import { useDirectus } from 'directus-react'

export const TodoItem = ({ item }) => {
  const { DirectusAsset } = useDirectus();

  return (
    <div>
      <h1>Todo #{item.id}</h1>
      <DirectusAsset asset={item.attachment}
        download={true}
        render={({ asset, url }) => <a href={url}>{asset.filename_download}</a>} />
    </div>
  );
};

<DirectusImage>

Computes the URL of the given resource asset, rendering it using the render prop:

  • asset: the asset representing the resource (string or object with an id property)
  • render: a function (which receive an object with the url property) that should return the component to render
  • fit: fit of the thumbnail while always preserving the aspect ratio, can be any of the following options: cover, contain, inside or outside
  • height: height of the thumbnail in pixels
  • quality: quality of the thumbnail (1 to 100)
  • width: width of the thumbnail in pixels
import React from 'react';
import { useDirectus } from 'directus-react'

export const TodoItem = ({ item }) => {
  const { DirectusImage } = useDirectus();

  return (
    <div>
      <DirectusImage asset={item.image}
        fit="cover" quality="75"
        render={({ asset, url }) => <img src={url} alt={asset.title} />} />
      <h1>Todo #{item.id}</h1>
    </div>
  );
};

❤️ Contributing

New features and bugfix are always welcome! In order to contribute to this project, follow a few easy steps:

  1. Fork this repository, clone it on your machine and run npm install
  2. Open your local repository with Visual Studio Code and install all the suggested extensions
  3. Create a branch my-awesome-feature and commit to it
  4. Run npm run lint and npm run build and verify that they complete without errors
  5. Push my-awesome-feature branch to GitHub and open a pull request

react-directus's People

Contributors

renovate-bot avatar gremo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.