{"id":16302605,"url":"https://github.com/toyobayashi/reactive-react","last_synced_at":"2025-04-10T05:57:28.754Z","repository":{"id":57168474,"uuid":"387747513","full_name":"toyobayashi/reactive-react","owner":"toyobayashi","description":"让 react 用上 @vue/reactivity","archived":false,"fork":false,"pushed_at":"2021-08-13T03:04:33.000Z","size":194,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-17T21:43:02.346Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/toyobayashi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-20T09:56:40.000Z","updated_at":"2021-08-17T07:58:48.000Z","dependencies_parsed_at":"2022-08-29T01:42:03.976Z","dependency_job_id":null,"html_url":"https://github.com/toyobayashi/reactive-react","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/toyobayashi%2Freactive-react","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toyobayashi%2Freactive-react/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toyobayashi%2Freactive-react/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toyobayashi%2Freactive-react/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/toyobayashi","download_url":"https://codeload.github.com/toyobayashi/reactive-react/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248166945,"owners_count":21058480,"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":[],"created_at":"2024-10-10T20:58:25.847Z","updated_at":"2025-04-10T05:57:28.732Z","avatar_url":"https://github.com/toyobayashi.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# reactive-react\n\n[API Documentation](https://github.com/toyobayashi/reactive-react/blob/main/docs/api/index.md)\n\n[中文](https://github.com/toyobayashi/reactive-react/blob/main/test)\n\n## Example\n\n### Hooks\n\n```jsx\nimport * as React from 'react'\nimport { ref, computed } from '@vue/reactivity'\nimport { useData, useRender } from '@tybys/reactive-react'\n\nfunction Counter () {\n  const data = useData(() =\u003e {\n    const localCount = ref(0)\n    const localDoubleCount = computed(() =\u003e localCount.value * 2)\n    const onClick = () =\u003e {\n      localCount.value++\n    }\n    return {\n      localCount,\n      localDoubleCount,\n      onClick\n    }\n  })\n\n  return useRender(() =\u003e\n    \u003cdiv\u003e\n      {data.localCount.value} * 2 = {data.localDoubleCount.value}\n      \u003cbutton onClick={data.onClick}\u003e+\u003c/button\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n### Class\n\n```jsx\nimport * as React from 'react'\nimport { ref, computed } from '@vue/reactivity'\nimport { ReactiveComponent } from '@tybys/reactive-react'\n\nclass Counter extends ReactiveComponent {\n  constructor (props) {\n    super(props) // onCreateReactiveData will be called\n\n    this.onClick = () =\u003e {\n      this.localCount.value++\n    }\n  }\n\n  /** @override */\n  onCreateReactiveData () {\n    // all reactive effect should be collected here\n    this.localCount = ref(0)\n    this.localDoubleCount = computed(() =\u003e this.localCount.value * 2)\n  }\n\n  render () {\n    return this.renderReactive(() =\u003e\n      \u003cdiv\u003e\n        {this.localCount.value} * 2 = {this.localDoubleCount.value}\n        \u003cbutton onClick={this.onClick}\u003e+\u003c/button\u003e\n      \u003c/div\u003e\n    )\n  }\n\n  /** @override */\n  componentWillUnmount () {\n    // ...\n    super.componentWillUnmount()\n  }\n})\n```\n\n### HOC\n\n```jsx\nimport * as React from 'react'\nimport { AnyComponent } from 'xx-react-component-library'\nimport { makeReactive } from '@tybys/reactive-react'\n\nconst ReactiveAnyComponent = makeReactive(AnyComponent, (props) =\u003e ([\n  props.xxProp,\n  propsx.xxProp2\n]))\n\n\u003cReactiveAnyComponent xxProp={yourReactiveData} xxProp2={yourReactiveData2} /\u003e\n```\n\n### Global State Management\n\nLike vuex:\n\n```jsx\nimport * as React from 'react'\nimport { createStore } from '@tybys/reactive-react'\n\nconst store = createStore({\n  state: {\n    count: 0\n  },\n  getters: {\n    doubleCount (state /*, getters */) {\n      return state.count * 2\n    }\n  },\n  mutations: {\n    add (state, value = 1) {\n      state.count += value\n    },\n    multi (state, value = 2) {\n      state.count *= value\n    }\n  },\n  actions: {\n    multi ({ commit /*, state, getters, dispatch */}, value = 2) {\n      return new Promise((resolve) =\u003e {\n        setTimeout(() =\u003e {\n          commit('multi', value)\n          resolve()\n        }, 200)\n      })\n    }\n  }\n})\n\nfunction Counter () {\n  const data = useData(() =\u003e {\n    const commit = () =\u003e {\n      store.mutations.add(10)\n      // or store.commit('add', 10)\n    }\n    const dispatch = () =\u003e {\n      store.actions.multi(5)\n      // or store.dispatch('multi', 5)\n    }\n    return { commit, dispatch }\n  })\n\n  return useRender(() =\u003e\n    \u003cdiv\u003e\n      {store.state.count} * 2 = {store.getters.doubleCount}\n      \u003cbutton onClick={data.commit}\u003e+\u003c/button\u003e\n      \u003cbutton onClick={data.dispatch}\u003ex\u003c/button\u003e\n    \u003c/div\u003e\n  )\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoyobayashi%2Freactive-react","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoyobayashi%2Freactive-react","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoyobayashi%2Freactive-react/lists"}