-
Notifications
You must be signed in to change notification settings - Fork 46.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array of references. #1899
Comments
It's not currently possible, though you could do |
👍 As well as giving each ref a unique name as @chenglou said, you can access all of the components refs with something like |
This will likely be more possible after #1373. |
Going to close out to try open issues down (even if we're currently failing). Let's make it an explicit goal/non-goal of #1373. |
@arcanis Yes, it is possible, setup a callback ref and save the ref to an array. For more info, check out the docs (https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute) or ask a usage question on stack overflow :). |
@jimfb I thought of that, but what if the number of elements shrinks? When should the extraneous elements be removed? And I asked here because it will be the first place people will find ("react array of refs" on Google) :) |
@arcanis We use github issues to track bugs in the React core, and explicitly send usage questions to StackOverflow. We encourage cross-linking, so feel free to post a link to you SO question so future travelers can go to the same place. To answer your question of when elements should be removed, the answer is "when the callback ref fires a null". You can create a closure to capture the name/identity/key in your callback function, and access/remove that value when the ref fires. |
Inserting a full working example here for others' reference class MyComponent extends React.Component {
constructor(props) {
super(props);
this._nodes = new Map();
}
componentDidMount() {
this.checkNodes();
}
componentDidUpdate() {
this.checkNodes();
}
checkNodes() {
Array.from(this._nodes.values())
.filter(node => node != null)
.forEach(node => {
// do something with node
});
}
render() {
const { values } = this.props;
return (
<div>
{values.map((value, i) => (
<div key={i} ref={c => this._nodes.set(i, c)}>{value}</div>
))}
</div>
)
}
} |
@harmony7 I use your example and it works for filtering but what if I need use simulation of currentElement.click() |
Like this? (Trying to make a setup similar to https://facebook.github.io/react/docs/refs-and-the-dom.html#adding-a-ref-to-a-dom-element ) class MyComponent extends React.Component {
constructor(props) {
super(props);
this._nodes = new Map();
this._handleClick = this.handleClick.bind(this);
}
handleClick(e, i) {
// Explicitly focus the text input using the raw DOM API
const node = this._nodes.get(i);
node.focus();
}
render() {
const { values } = this.props;
return (
<div>
{values.map((value, i) => (
<div key={i}>
<input type="text"
value={value}
ref={c => this._nodes.set(i, c)} />
<input type="button"
value="Focus the text input"
onClick={e => this.handleClick(e, i)} />
</div>
))}
</div>
)
}
} Just so you know, this example is for illustration of this concept only and it does not follow some best practices, for example it is probably not the best to create the inline arrow function to handle |
The inline arrow function is fine here. |
for React 16+ |
@chenglou 's solution still works even today in 2019 (O_o) thanks. |
--- #1899 only propagated eliminated phi nodes to function expressions on the first iteration through blocks. However, this was buggy as later iterations could introduce rewrites that need to be propagated. [playground repro](https://0xeac7-forget.vercel.app/#eyJzb3VyY2UiOiJmdW5jdGlvbiBDb21wb25lbnQoKSB7XG4gIGNvbnN0IHggPSA0O1xuXG4gIGNvbnN0IGdldDQgPSAoKSA9PiB7XG4gICAgd2hpbGUgKGJhcigpKSB7XG4gICAgICBpZiAoYmF6KSB7XG4gICAgICAgIGJhcigpO1xuICAgICAgfVxuICAgIH1cbiAgICByZXR1cm4gKCkgPT4geDtcbiAgfTtcblxuICByZXR1cm4gZ2V0NDtcbn0ifQ==). I manually synced #1907 to check that this fix works for the VR Store codebase.
E.g, when I generate list of buttons, I want to assign every button to same array, which is accessible through
this.refs
. After that, I can perform operation on every button easily. Something like square bracket syntax<input type="text" name="input[]" />
in html.The text was updated successfully, but these errors were encountered: