{"id":15011829,"url":"https://github.com/developit/linkref","last_synced_at":"2025-04-12T03:30:59.615Z","repository":{"id":47622927,"uuid":"71726238","full_name":"developit/linkref","owner":"developit","description":"Like Linked State, but for Refs. Works with Preact and React.","archived":false,"fork":false,"pushed_at":"2019-01-31T16:22:43.000Z","size":8,"stargazers_count":61,"open_issues_count":2,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-10T11:16:08.342Z","etag":null,"topics":["linkref","preact","react"],"latest_commit_sha":null,"homepage":"https://npm.im/linkref","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/developit.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-10-23T19:55:39.000Z","updated_at":"2021-09-10T11:30:12.000Z","dependencies_parsed_at":"2022-08-29T23:21:40.672Z","dependency_job_id":null,"html_url":"https://github.com/developit/linkref","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/developit%2Flinkref","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Flinkref/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Flinkref/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Flinkref/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developit","download_url":"https://codeload.github.com/developit/linkref/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248512497,"owners_count":21116613,"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":["linkref","preact","react"],"created_at":"2024-09-24T19:41:46.407Z","updated_at":"2025-04-12T03:30:59.336Z","avatar_url":"https://github.com/developit.png","language":"JavaScript","readme":"# `linkref` [![NPM](https://img.shields.io/npm/v/linkref.svg?style=flat)](https://www.npmjs.org/package/linkref) [![travis-ci](https://travis-ci.org/developit/linkref.svg?branch=master)](https://travis-ci.org/developit/linkref)\n\n\u003e Like [Preact]'s [Linked State], but for [refs].\n\n\u003cimg src=\"http://i.imgur.com/V4kTgbn.png\" width=\"447\"\u003e\n\nThis gives you **the ease of String Refs** _(unavailable in Preact core, deprecated in React)_, **using Function Refs**.\n\nCalling `linkRef('name')` creates an optimized ref handler function that populates `this.refs.name` on your component for you. The trick is that `linkRef()` is **memoized** - this means you can call it as many times as you want (inline, in render!) and it won't create any new closures. 🌈\n\n\u003e **Note:** You can accomplish this with function refs just fine! It's important to understand how function refs work, since `linkRef()` is just generating a function ref on your behalf. Here is an unoptimized implementation of `linkRef()` - be sure to understand how it works:\n\u003e\n\u003e ```js\n\u003e function linkRef(component, name) {\n\u003e   if (!component.refs) component.refs = {};\n\u003e   return (ref) =\u003e component.refs[name] = ref;\n\u003e }\n\u003e ```\n\n\n---\n\n\n### Installation\n\nAvailable as [linkref on npm](https://npm.im/linkref):\n\n```sh\nnpm install --save linkref\n```\n\n\n### Simple Example\n\n```js\nimport linkRef from 'linkref';\n\nclass Foo extends Component {\n    componentDidMount() {\n        // log the \u003cdiv /\u003e to the console:\n        console.log(this.refs.foo);\n    }\n    render() {\n        return (\n            \u003cdiv ref={linkRef(this, 'foo')}\u003e\n                some text\n            \u003c/div\u003e\n        );\n    }\n}\n```\n\n\n### Preact Polyfill\n\n[Preact]'s `Component` class is extensible, so `linkref` provides a polyfill to integrate more tightly:\n\n```js\nimport 'linkref/polyfill';\n\nclass Foo extends Component {\n    componentDidMount() {\n        // log the \u003cdiv /\u003e to the console:\n        console.log(this.refs.foo);\n    }\n    render() {\n        return (\n            \u003cdiv ref={this.linkRef('foo')}\u003e\n                some text\n            \u003c/div\u003e\n        );\n    }\n}\n```\n\n\n### Babel Plugin\n\nThere's also a Babel plugin available as `linkref/babel`:\n\n```js\n// .babelrc\n{\n  \"plugins\": [\n    \"linkref/babel\",\n\n    // all options are optional - here are the defaults:\n    [\"linkref/babel\", {\n      \"module\": \"linkref\",\n      \"importName\": \"_createStringRef\"\n    }]\n  ]\n}\n```\n\nIt's also useful as a one-off codemod for upgrading from string refs to linkref:\n\n```sh\nnpm i --save linkref\n\n# run babel on all files in ./src, changing them in-place:\nnpx babel --no-babelrc --plugins=linkref/babel src -d src\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eDon't have \u003ccode\u003enpx\u003c/code\u003e?\u003c/summary\u003e\n\nUpdate to the latest `npm` version to get it, or do this instead:\n\n```sh\n# get babel cli:\nnpm i @babel/core @babel/cli\n# you'll be using linkref as a library now:\nnpm i --save linkref\n# run babel on all files in ./src, changing them in-place:\n./node_modules/.bin/babel --no-babelrc --plugins=linkref/babel src -d src\n```\n\u003c/details\u003e\n\n---\n\n\n### License\n\nMIT\n\n\n[Preact]: https://github.com/developit/preact\n[Linked State]: https://preactjs.com/guide/linked-state\n[refs]: https://facebook.github.io/react/docs/refs-and-the-dom.html\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Flinkref","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelopit%2Flinkref","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Flinkref/lists"}