{"id":21202568,"url":"https://github.com/state-less/react-client","last_synced_at":"2025-12-30T12:57:08.460Z","repository":{"id":147217619,"uuid":"342373510","full_name":"state-less/react-client","owner":"state-less","description":"Discover @state-less/react-client, a library to connect server-side components with client-side React applications. Simplify frontend development and create powerful full-stack apps with ease. Get started now!","archived":false,"fork":false,"pushed_at":"2024-03-23T15:18:56.000Z","size":1595,"stargazers_count":0,"open_issues_count":5,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-21T15:11:24.650Z","etag":null,"topics":["javascript","react-server","reactjs","typescript"],"latest_commit_sha":null,"homepage":"https://state-less.cloud","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/state-less.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-02-25T20:42:10.000Z","updated_at":"2023-04-27T20:29:00.000Z","dependencies_parsed_at":"2024-03-23T16:25:37.732Z","dependency_job_id":"654dd60c-49bd-4c1c-af0e-086553e17fc3","html_url":"https://github.com/state-less/react-client","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-less%2Freact-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-less%2Freact-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-less%2Freact-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-less%2Freact-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/state-less","download_url":"https://codeload.github.com/state-less/react-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243655581,"owners_count":20326116,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","react-server","reactjs","typescript"],"created_at":"2024-11-20T20:17:06.034Z","updated_at":"2025-12-30T12:57:08.436Z","avatar_url":"https://github.com/state-less.png","language":"HTML","readme":"# @state-less/react-client\n\n![npm (scoped)](https://img.shields.io/npm/v/@state-less/react-client)\n\n`@state-less/react-client` is a powerful framework that allows you to build and manage server-rendered React applications with ease. It comes with built-in support for server-side state management, authentication, and more.\n\nFor detailed documentation and in-depth guides, please visit the official website at [state-less.cloud](https://state-less.cloud).\n\n## Getting Started\n\n### Backend\n\nTo get started on the backend, you can initialize a new project with the following commands:\n\n```bash\ngit clone https://github.com/state-less/clean-starter.git -b react-server my-server\n```\n\nThis command will set up a new project with the necessary dependencies and configuration files. Navigate into the newly created directory and start the development server:\n\n```bash\ncd my-server\ngit remote remove origin\nyarn install\nyarn start\n```\n\nThis will launch the development server, allowing you to access your GraphQl endpoint at http://localhost:4000/graphql.\n\n### Client\n\n### Get a Client running\n\nCreate a new vite project and choose _React_ as framework and _TypeScript_ as variant.\n\n```bash\nyarn create vite\n```\n\nNow go to the newly created folder, install the dependencies and add `@apollo/client` and `state-less/react-client` to your project and start the server.\n\n```bash\ncd vite-project\nyarn\nyarn add @apollo/client state-less/react-client\nyarn dev\n```\n\n![screenshot](https://raw.githubusercontent.com/state-less/react-server-docs-md/master/images/screenshot.jpg)\n\nIf you click the button, you will see the counter increase, but if you reload the page, the counter resets to 0. Let's connect the state to our backend to make it serverside and persist over page reloads.\n\n#### Instantiate a GraphQl client\n\nIn order to connect to our backend, we need to create a GraphQl client. Create a new file under `/src/lib/client.ts` and paste the following content.\n\n```ts\nimport { ApolloClient, InMemoryCache, split, HttpLink } from '@apollo/client';\nimport { WebSocketLink } from '@apollo/client/link/ws';\nimport { getMainDefinition } from '@apollo/client/utilities';\n\n// Create an HTTP link\nconst localHttp = new HttpLink({\n    uri: 'http://localhost:4000/graphql',\n});\n\n// Create a WebSocket link\nconst localWs = new WebSocketLink({\n    uri: `ws://localhost:4000/graphql`,\n    options: {\n        reconnect: true,\n    },\n});\n\n// Use the split function to direct traffic between the two links\nconst local = split(\n    ({ query }) =\u003e {\n        const definition = getMainDefinition(query);\n        return (\n            definition.kind === 'OperationDefinition' \u0026\u0026\n            definition.operation === 'subscription'\n        );\n    },\n    localWs,\n    localHttp\n);\n\n// Create the Apollo Client instance\nexport const localClient = new ApolloClient({\n    link: local,\n    cache: new InMemoryCache(),\n});\n\nexport default localClient;\n```\n\nThis sets up a new GraphQl client with subscriptions which will be used by the React Server client. The subscriptions are needed in order to make your app _reactive_.\n\n_Note: For now you need to manually create this file, but it will later be created by an initializer or react-client will provide a way to bootstrap the graphql client by providing a url pointing to a react server. For now you need to manually create and provide a GraphQl client._\n\n### Edit `src/App.tsx`\n\nIt's been a long way, but all that's left to do is import the `client` and `useServerState` hook and find and replace the `useState` call with a `useServerState` call.\n\n```ts\nimport { useServerState } from '@state-less/react-client';\nimport client from './lib/client';\n\n// ...\n\nconst [count, setCount] = useServerState(0, {\n    key: 'count',\n    scope: 'global',\n    client,\n});\n```\n\nIf you don't want to pass a client object to each query, you can wrap your application in an `\u003cApolloprovider client={client} /\u003e`. React Server will use the provided client.\n_Note: You can still override the provided client if you pass one in the options_\n\nThat's it, your App is now powered by the same backend as the documentation under [state-less.cloud](https://state-less.cloud).\n\nHappy Hacking!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstate-less%2Freact-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstate-less%2Freact-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstate-less%2Freact-client/lists"}