{"id":15413054,"url":"https://github.com/fahad19/tydel-react","last_synced_at":"2025-04-19T11:38:06.403Z","repository":{"id":57383112,"uuid":"64038346","full_name":"fahad19/tydel-react","owner":"fahad19","description":"React.js bindings for Tydel","archived":false,"fork":false,"pushed_at":"2016-08-08T20:00:38.000Z","size":25,"stargazers_count":6,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T15:05:01.843Z","etag":null,"topics":["react","react-bindings","state-management","tydel"],"latest_commit_sha":null,"homepage":"http://tydel.js.org/integrations/react/index.html","language":"JavaScript","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/fahad19.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-23T22:22:06.000Z","updated_at":"2016-11-15T17:00:53.000Z","dependencies_parsed_at":"2022-08-30T20:01:11.844Z","dependency_job_id":null,"html_url":"https://github.com/fahad19/tydel-react","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fahad19%2Ftydel-react","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fahad19%2Ftydel-react/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fahad19%2Ftydel-react/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fahad19%2Ftydel-react/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fahad19","download_url":"https://codeload.github.com/fahad19/tydel-react/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249675922,"owners_count":21309363,"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":["react","react-bindings","state-management","tydel"],"created_at":"2024-10-01T16:55:17.680Z","updated_at":"2025-04-19T11:38:06.368Z","avatar_url":"https://github.com/fahad19.png","language":"JavaScript","readme":"# tydel-react\n\u003c!--{h1:.massive-header.-with-tagline}--\u003e\n\n\u003e React bindings for Tydel\n\n[![Build Status](https://img.shields.io/travis/fahad19/tydel-react/master.svg)](http://travis-ci.org/fahad19/tydel-react) [![npm](https://img.shields.io/npm/v/tydel-react.svg)](https://www.npmjs.com/package/tydel-react)\n\nAllows you to use [Tydel](https://github.com/fahad19/tydel) for managing your state in [React.js](https://github.com/facebook/react) applications.\n\n## Installation\n\n### npm\n\nWith [npm](https://npmjs.com):\n\n```\n$ npm install --save tydel-react\n```\n\n### Bower\n\nWith [Bower](https://bower.io):\n\n```\n$ bower install --save tydel-react\n```\n\nIn your HTML file:\n\n```js\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n  \u003cbody\u003e\n    \u003cscript src=\"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js\"\u003e\u003c/script\u003e\n    \u003cscript src=\"https://cdnjs.cloudflare.com/ajax/libs/react/15.2.1/react.min.js\"\u003e\u003c/script\u003e\n    \u003cscript src=\"https://cdnjs.cloudflare.com/ajax/libs/react/15.2.1/react-dom.min.js\"\u003e\u003c/script\u003e\n\n    \u003cscript src=\"bower_components/tydel/dist/tydel.min.js\"\u003e\u003c/script\u003e\n    \u003cscript src=\"bower_components/tydel-react/dist/tydel-react.min.js\"\u003e\u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n## Usage\n\nImport the modules first:\n\n```js\n// React\nimport { PropTypes, Component } from 'react';\nimport { render } from 'react-dom';\n\n// Tydel\nimport { Types, createModel } from 'tydel';\nimport { Provider, connect } from 'tydel-react';\n```\n\nLet's define and instantiate a Model which will act as our state for the React application:\n\n```js\n// Model class\nconst AppState = createModel({\n  name: Types.string,\n}, {\n  setName(name) {\n    this.name = name;\n  }\n});\n\n// instance\nconst appState = new AppState({\n  name: 'My new app'\n});\n```\n\nNow that we have the `appState` model instance, let's create our root Component:\n\n```js\nclass AppComponent extends Component {\n  render() {\n    const { name, setName } = this.props;\n\n    \u003cdiv\u003e\n      \u003cp\u003e\n        App name is: {name}\n      \u003c/p\u003e\n\n      {/* Clicking here would update the name, and re-render the Component */}\n      \u003ca onClick={() =\u003e setName('foo')}\u003e\n        Click to set app name to `foo`\n      \u003c/a\u003e\n    \u003c/div\u003e\n  }\n}\n```\n\nTo inject `name` and `setName` as props to the Component, we need to decorate it with `connect` function:\n\n```js\n// `AppComponent` variable is now `App` after connecting\nconst App = connect(function mapModelToProps(model) {\n  // `model` is `appState`\n  return {\n    name: model.name,\n    setName: model.setName\n  };\n\n  // or we could just `return model;` here\n})(AppComponent);\n```\n\nNow it's time to render it to DOM. Here we are gonna use the `\u003cProvider\u003e` component and pass our `appState` as the model, so that all child Components, when using `connect()`, would be able to access the state:\n\n```js\nrender(\n  \u003cProvider model={appState}\u003e\n    \u003cApp /\u003e\n  \u003c/Provider\u003e,\n  document.getElementById('root') // mounts the app in \u003cdiv id=\"root\"\u003e\u003c/div\u003e\n);\n```\n\nAnd you have a working React application with Tydel!\n\n## API\n\n### `\u003cProvider model\u003e`\n\nThe root component of your application needs to be wrapped with `\u003cProvider\u003e` in order to pass the model around via React's context API.\n\nTo be imported as:\n\n```js\nimport { Provider } from 'tydel-react';\n```\n\nAccepts only one prop called `model`. Pass your model instance there.\n\n```js\nimport React from 'react';\nimport { render } from 'react-dom';\n\nconst rootElement = document.getElementById('root');\n\nconst model = new Model({...}); // your own Model class created by Tydel\nconst App = React.createComponent({...}); // your root Component\n\nrender(\n  \u003cProvider model={model}\u003e\n    \u003cApp /\u003e\n  \u003c/Provider\u003e\n);\n```\n\n### `connect(mapModelToProps)`\n\nThis function accepts a function `mapModelToProps`, which then accepts the model instance we initially passed via `\u003cProvider model={model}\u003e`, and returns an object which is then injected as props in your custom Component.\n\nImagine your `mapModelToProps` function as this:\n\n```js\nfunction mapModelToProps(model) {\n  return {\n    name: model.name,\n    setName: model.setName\n  };\n}\n```\n\nNow if you had your root component in a variable called `AppComponent`, we could connect it as:\n\n```js\n// React component\nconst AppComponent = React.createClass({...});\n\n// connected component\nconst App = connect(mapModelToProps)(AppComponent);\n```\n\nNow, when the `App` component gets rendered somewhere, it would have access to `name` and `setName` in its props as `this.props.name` for example.\n\n## License\n\nMIT © [Fahad Ibnay Heylaal](http://fahad19.com)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffahad19%2Ftydel-react","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffahad19%2Ftydel-react","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffahad19%2Ftydel-react/lists"}