├── .gitignore ├── .gtihub └── workflows │ └── main-ci.yml ├── .prettierrc ├── LICENSE ├── README.md ├── babel.config.cjs ├── jest.config.cjs ├── package-lock.json ├── package.json ├── resolver.cjs ├── src └── index.ts ├── test └── react-solid-state.spec.tsx └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist -------------------------------------------------------------------------------- /.gtihub/workflows/main-ci.yml: -------------------------------------------------------------------------------- 1 | name: 'React Solid State CI' 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - '*' 7 | push: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | job: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: 14 19 | 20 | - name: Installing deps 21 | run: npm install 22 | 23 | - name: Building lib 24 | run: npm run build 25 | 26 | - name: Testing & Coverage 27 | run: | 28 | npm run test 29 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "none", 3 | "tabWidth": 2, 4 | "semi": true, 5 | "singleQuote": false, 6 | "arrowParens": "avoid", 7 | "printWidth": 100 8 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018-2021 Ryan Carniato 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Solid State 2 | 3 | [](https://github.com/solidjs/react-solid-state/actions/workflows/main-ci.yml) 4 | [](https://www.npmjs.com/package/react-solid-state) 5 |  6 | 7 | This is a local state swap for React using [SolidJS](https://github.com/solidjs/solid). Instead of worrying about when your components should update you can use declarative data. This makes use of the new React Hooks API. However it differs in a few really key ways: 8 | - Dependencies are automatically tracked. While there is an option to set explicit dependencies it is isn't necessary. 9 | - Nested hooks are allowed. Effects that produce sub nested effects are fair game. 10 | 11 | The goal here is to give as close as possible to Solid's easy state management and fine-grained dependency detection while still being able to use React. All of [Solid's API methods](https://www.solidjs.com/docs/latest/api) have been ported. Note: this uses React Hooks so it only works with Function Components which is consistent with how Components work in Solid. 12 | 13 | There are a few differences in the Solid API from some React Hooks of the same name. Solid `Store`s are objects much like traditional React State. There is a `useCleanup` method that lets you register release code at both the component unmount level and in each Hook. `useEffect` doesn't expect a cleanup/dispose method returned for that reason. `useMemo` (and `useSignal`) return getters rather than the the pure value. This is because the context where data gets accessed is the key to automatic dependency tracking. For all the information of how Solid works, look at the [website](https://solidjs.com). 14 | 15 | To get started, simply wrap your components in `withSolid` as a HOC, and have your Component return a Function with your JSX. From there use your hooks. 16 | 17 | This package exports both direct Solid API named functions like `createEffect`, and `use`-prefixed ones. Solid isn't subject to the Hook rules, so it makes sense to use `create` prefixes, but if you want to use `use` you can. 18 | 19 | ```jsx 20 | import { withSolid, createSignal } from 'react-solid-state' 21 | import React from 'react' 22 | 23 | const WelcomeComponent = withSolid(props => { 24 | const [recipient, setRecipient] = createSignal('John'); 25 | return () => (
( 80 | ComponentType: (props: PropsWithChildren
, r: any) => () => JSX.Element 81 | ) { 82 | return memo
((p, r) => {
83 | const component = ComponentType(p, r);
84 | return (component && useObserver(component)) || null;
85 | });
86 | }
87 |
88 | export function useStore