Skip to content

Commit f2253e3

Browse files
authored
Read/write refs on this (#125)
Fixes #123 Using the `ref` prop writes the ref to the `this.refs` object. However, setting a ref with a callback is not allowed to write to the frozen `this.refs`. The `writeRef` props writes directly to `this` instead.
1 parent b7b7a99 commit f2253e3

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

Diff for: src/React.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ exports.getChildren = getChildren;
4343
function readRefImpl (this_) {
4444
return function(name) {
4545
return function() {
46-
var refs = this_.refs || {};
47-
return refs[name];
46+
return this_[name];
4847
}
4948
}
5049
}
@@ -54,9 +53,7 @@ function writeRef(this_) {
5453
return function(name) {
5554
return function(node) {
5655
return function() {
57-
var refs = this_.refs || {};
58-
refs[name] = node;
59-
this_.refs = refs;
56+
this_[name] = node;
6057
return {};
6158
}
6259
}

Diff for: src/React/DOM/Props.purs

+4-1
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,16 @@ radioGroup = unsafeMkProps "radioGroup"
301301
readOnly :: Boolean -> Props
302302
readOnly = unsafeMkProps "readOnly"
303303

304+
-- | You can use `ref` to store a reference on `this.refs`.
305+
-- | To access the stored reference, `getRefs` can then be used.
304306
ref :: String -> Props
305307
ref = unsafeMkProps "ref"
306308

307-
-- | You can use `writeRef` to store a reference on `Refs`.
309+
-- | You can use `writeRef` to store a reference on `this`.
308310
-- | ```purescript
309311
-- | div [ withRef (writeRef this "inputElement") ] [...]
310312
-- | ```
313+
-- | To access the stored reference, `readRef` can then be used.
311314
withRef
312315
:: forall access eff
313316
. (Nullable Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit)

0 commit comments

Comments
 (0)