{"id":21202572,"url":"https://github.com/state-less/clean-starter","last_synced_at":"2025-07-10T06:33:09.460Z","repository":{"id":147217617,"uuid":"463054410","full_name":"state-less/clean-starter","owner":"state-less","description":"Explore the power of React Server with this collection of essential backend examples. Get started quickly and learn the best practices for implementing a wide range of functionalities with React Server. Dive into real-world use cases and discover how to build modular, component-driven server-side applications with ease. Happy coding!","archived":false,"fork":false,"pushed_at":"2024-02-27T19:24:46.000Z","size":1691,"stargazers_count":12,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-01T11:44:26.994Z","etag":null,"topics":["javascript","react-server","reactjs","typescript"],"latest_commit_sha":null,"homepage":"https://state-less.cloud","language":"TypeScript","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":"2022-02-24T07:45:28.000Z","updated_at":"2024-03-28T15:11:30.000Z","dependencies_parsed_at":"2024-02-18T17:25:20.296Z","dependency_job_id":"a6e37e18-b3be-4dbb-a9d3-8e007ceb2828","html_url":"https://github.com/state-less/clean-starter","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%2Fclean-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-less%2Fclean-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-less%2Fclean-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-less%2Fclean-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/state-less","download_url":"https://codeload.github.com/state-less/clean-starter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225622826,"owners_count":17498170,"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.324Z","updated_at":"2024-11-20T20:17:07.214Z","avatar_url":"https://github.com/state-less.png","language":"TypeScript","readme":"The @state-less/clean-starter repository is designed to provide a starting point for developers exploring React Server. It includes essential backend components, utilities, and examples that illustrate the fundamental concepts and capabilities of React Server.\n\n# React Server\nReact Server allows the creation of server-side React components using TSX, promoting a component-driven architecture for building robust and maintainable backend solutions. By using @state-less/clean-starter, developers can quickly get started with this innovative approach to full-stack development.\n\n```tsx\nimport { Scopes, useState, clientKey } from '@state-less/react-server';\nimport { ServerSideProps } from './ServerSideProps';\n\nexport const HelloWorldExample2 = (props, { key, context })  =\u003e {\n  // The useState hook looks familiar?\n  const [count, setState] = useState(0, {\n    key: \"count\",\n    scope: Scopes.Global,\n  });\n\n  // A simple function that can be executed from the client side.\n  const increase = () =\u003e {\n    setState(count + 1);\n  };\n\n  return (\n    // Simply pass down props to the client side.\n    \u003cServerSideProps\n      key={clientKey(`${key}-props`, context)}\n      count={count}\n      increase={increase}\n    /\u003e\n  );\n};\n```\n\nUse the familiar approach of React to build serverside applications in a declarative way. React has proven to be a successful framework for building large scale applications while keeping a clean state-management solution to provide reactive realtime enabled modern UIs. React favors a clean codebase due to its inherently modular structure and component driven approach. React allows you to cleanly abstract the complexity of modern applications using *states*, *hooks* and *components*. \n\nReact Server brings the same flexibility to the server side by giving you a Framework that handles TSX compilation, a small React engine that powers *hooks*, *states* and the lifecycle of *components*. It also abstracts data storage and data transportation into a common concept of *states*. Data is stored in *Stores* which can be accessed by components using *hooks* (`useState`). The `useState` hook provides reactivity and automatically re-renders the component when its value is updated using the setter. Data is transported by consuming server-side states / components on the clientside using the `useComponent` hook. We are using *GraphQL* as transportation layer to provide a robust foundation. \n\nDevelopers familiar with **React** and JSX components `\u003cComponent hello=\"world\" /\u003e` are able to jump right into developing full-stack applications. It really works well and provides a seamless reactive experience on both the backend and the frontend. States are synchronized to all connected clients using PUB / SUB. \n\nFor detailed documentation and in-depth guides, please visit the official website at [state-less.cloud](https://state-less.cloud).\n\n## Explore Examples\nDive into real-world examples showcasing the potential of React Server.\n\n### Projects using React Server\n* [Lists](https://lists.state-less.cloud)\n* [JavaScript Forum](https://javascript.forum)\n* [React Server Docs](https://state-less.cloud)\n\n## Key Features\n- **Server-Side React**: Create server-side React components using TSX for efficient, component-driven backend development.\n- **@state-less/clean-starter**: Jumpstart your project with essential backend components, utilities, and examples.\n- **Explore the Future**: Harness the power of server-side real-time TSX components.\n\nFor comprehensive documentation and detailed guides, visit the official [state-less.cloud](https://state-less.cloud) website.\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\n---------\n\n**License**: This project is under the **MIT License** - see the [LICENSE.md](/LICENSE.txt) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstate-less%2Fclean-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstate-less%2Fclean-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstate-less%2Fclean-starter/lists"}