Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-relay

Package Overview
Dependencies
Maintainers
8
Versions
3608
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-relay

A framework for building GraphQL-driven React applications.

  • 18.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
124K
increased by1.73%
Maintainers
8
Weekly downloads
 
Created

What is react-relay?

React Relay is a JavaScript framework for building data-driven React applications. It allows you to declaratively fetch data and manage it in a way that is tightly integrated with your React components. Relay is designed to work with GraphQL, a query language for APIs, and it helps in optimizing data fetching and updating the UI efficiently.

What are react-relay's main functionalities?

Declarative Data Fetching

This feature allows you to declaratively fetch data using GraphQL queries. The `QueryRenderer` component is used to fetch data and render the UI based on the fetched data.

```javascript
import React from 'react';
import { QueryRenderer, graphql } from 'react-relay';
import environment from './environment';

const App = () => (
  <QueryRenderer
    environment={environment}
    query={graphql`
      query AppQuery {
        viewer {
          id
          name
        }
      }
    `}
    render={({ error, props }) => {
      if (error) {
        return <div>Error: {error.message}</div>;
      }
      if (!props) {
        return <div>Loading...</div>;
      }
      return <div>User: {props.viewer.name}</div>;
    }}
  />
);

export default App;
```

Fragment Containers

Fragment Containers allow you to co-locate data dependencies with your React components. This means you can specify the data a component needs using GraphQL fragments, and Relay will ensure that the data is fetched and passed to the component.

```javascript
import React from 'react';
import { createFragmentContainer, graphql } from 'react-relay';

const UserComponent = ({ user }) => (
  <div>
    <h1>{user.name}</h1>
    <p>{user.email}</p>
  </div>
);

export default createFragmentContainer(UserComponent, {
  user: graphql`
    fragment UserComponent_user on User {
      name
      email
    }
  `,
});
```

Mutations

Mutations in Relay allow you to modify data on the server and update the client-side store. The `commitMutation` function is used to send a mutation to the server and handle the response.

```javascript
import { commitMutation, graphql } from 'react-relay';
import environment from './environment';

const mutation = graphql`
  mutation AddUserMutation($input: AddUserInput!) {
    addUser(input: $input) {
      user {
        id
        name
      }
    }
  }
`;

function addUser(name) {
  const variables = {
    input: {
      name,
    },
  };

  commitMutation(environment, {
    mutation,
    variables,
    onCompleted: (response, errors) => {
      console.log('Response received from server.', response, errors);
    },
    onError: err => console.error(err),
  });
}
```

Other packages similar to react-relay

Keywords

FAQs

Package last updated on 04 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc