ref not referencing correct item in array
up vote
0
down vote
favorite
This issue probably arises from my lack of understanding of ref
s.
I have this container that I am rendering from an array.
Lets say it will render 4 containers. Each container will have 3 links. When I hover over the one of the links a clipboard icon becomes visible. Using react-copy-to-clipboard
library I copy the contents of the that particular item to the clipboard. However, my issue is that it is only copying the last item of the 4th container instead of the one I hovered over or chose. I feel it might also have to do with the index?
I hope someone can clarify why this is happening and help me find a solution.
handleMouseEnter = index =>
this.setState(prevState => (
isHovered:
...prevState.isHovered,
[index]: true,
,
ref: this.textRef.current.attributes.label.nodeValue,
));
;
<LinkContainer className="linkContainer">
links.map(( name, path , index) => (
<ul className="styledLinks">
<li
onMouseEnter=() => this.handleMouseEnter(index)
onMouseLeave=() => this.handleMouseLeave(index)
>
<StyledLinks
key=name
ref=this.textRef
value=name
label=path
/>
isHovered[index] && (
<CopyToClipboard
onCopy=this.copy
text=ref
>
<StyledCopyIcon
icon="copy"
className="copyIcon"
copySucces=copySuccess
onClick=this.setCopySuccess
/>
</CopyToClipboard>
)
</li>
</ul>
))
</LinkContainer>
javascript arrays reactjs ref
add a comment |
up vote
0
down vote
favorite
This issue probably arises from my lack of understanding of ref
s.
I have this container that I am rendering from an array.
Lets say it will render 4 containers. Each container will have 3 links. When I hover over the one of the links a clipboard icon becomes visible. Using react-copy-to-clipboard
library I copy the contents of the that particular item to the clipboard. However, my issue is that it is only copying the last item of the 4th container instead of the one I hovered over or chose. I feel it might also have to do with the index?
I hope someone can clarify why this is happening and help me find a solution.
handleMouseEnter = index =>
this.setState(prevState => (
isHovered:
...prevState.isHovered,
[index]: true,
,
ref: this.textRef.current.attributes.label.nodeValue,
));
;
<LinkContainer className="linkContainer">
links.map(( name, path , index) => (
<ul className="styledLinks">
<li
onMouseEnter=() => this.handleMouseEnter(index)
onMouseLeave=() => this.handleMouseLeave(index)
>
<StyledLinks
key=name
ref=this.textRef
value=name
label=path
/>
isHovered[index] && (
<CopyToClipboard
onCopy=this.copy
text=ref
>
<StyledCopyIcon
icon="copy"
className="copyIcon"
copySucces=copySuccess
onClick=this.setCopySuccess
/>
</CopyToClipboard>
)
</li>
</ul>
))
</LinkContainer>
javascript arrays reactjs ref
What isref
? you're setting the copy text to that every time
– Andy Ray
Nov 8 at 16:59
Sorry, that was not clear enough. I am storing the ref to state based on theonMouseEnter
then I am using thatthis.state.ref
to copy as text. That is the incorrect text though, always grabs the same one. @AndyRay
– rowmna
Nov 8 at 17:05
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This issue probably arises from my lack of understanding of ref
s.
I have this container that I am rendering from an array.
Lets say it will render 4 containers. Each container will have 3 links. When I hover over the one of the links a clipboard icon becomes visible. Using react-copy-to-clipboard
library I copy the contents of the that particular item to the clipboard. However, my issue is that it is only copying the last item of the 4th container instead of the one I hovered over or chose. I feel it might also have to do with the index?
I hope someone can clarify why this is happening and help me find a solution.
handleMouseEnter = index =>
this.setState(prevState => (
isHovered:
...prevState.isHovered,
[index]: true,
,
ref: this.textRef.current.attributes.label.nodeValue,
));
;
<LinkContainer className="linkContainer">
links.map(( name, path , index) => (
<ul className="styledLinks">
<li
onMouseEnter=() => this.handleMouseEnter(index)
onMouseLeave=() => this.handleMouseLeave(index)
>
<StyledLinks
key=name
ref=this.textRef
value=name
label=path
/>
isHovered[index] && (
<CopyToClipboard
onCopy=this.copy
text=ref
>
<StyledCopyIcon
icon="copy"
className="copyIcon"
copySucces=copySuccess
onClick=this.setCopySuccess
/>
</CopyToClipboard>
)
</li>
</ul>
))
</LinkContainer>
javascript arrays reactjs ref
This issue probably arises from my lack of understanding of ref
s.
I have this container that I am rendering from an array.
Lets say it will render 4 containers. Each container will have 3 links. When I hover over the one of the links a clipboard icon becomes visible. Using react-copy-to-clipboard
library I copy the contents of the that particular item to the clipboard. However, my issue is that it is only copying the last item of the 4th container instead of the one I hovered over or chose. I feel it might also have to do with the index?
I hope someone can clarify why this is happening and help me find a solution.
handleMouseEnter = index =>
this.setState(prevState => (
isHovered:
...prevState.isHovered,
[index]: true,
,
ref: this.textRef.current.attributes.label.nodeValue,
));
;
<LinkContainer className="linkContainer">
links.map(( name, path , index) => (
<ul className="styledLinks">
<li
onMouseEnter=() => this.handleMouseEnter(index)
onMouseLeave=() => this.handleMouseLeave(index)
>
<StyledLinks
key=name
ref=this.textRef
value=name
label=path
/>
isHovered[index] && (
<CopyToClipboard
onCopy=this.copy
text=ref
>
<StyledCopyIcon
icon="copy"
className="copyIcon"
copySucces=copySuccess
onClick=this.setCopySuccess
/>
</CopyToClipboard>
)
</li>
</ul>
))
</LinkContainer>
javascript arrays reactjs ref
javascript arrays reactjs ref
edited Nov 8 at 17:04
asked Nov 8 at 16:49
rowmna
255
255
What isref
? you're setting the copy text to that every time
– Andy Ray
Nov 8 at 16:59
Sorry, that was not clear enough. I am storing the ref to state based on theonMouseEnter
then I am using thatthis.state.ref
to copy as text. That is the incorrect text though, always grabs the same one. @AndyRay
– rowmna
Nov 8 at 17:05
add a comment |
What isref
? you're setting the copy text to that every time
– Andy Ray
Nov 8 at 16:59
Sorry, that was not clear enough. I am storing the ref to state based on theonMouseEnter
then I am using thatthis.state.ref
to copy as text. That is the incorrect text though, always grabs the same one. @AndyRay
– rowmna
Nov 8 at 17:05
What is
ref
? you're setting the copy text to that every time– Andy Ray
Nov 8 at 16:59
What is
ref
? you're setting the copy text to that every time– Andy Ray
Nov 8 at 16:59
Sorry, that was not clear enough. I am storing the ref to state based on the
onMouseEnter
then I am using that this.state.ref
to copy as text. That is the incorrect text though, always grabs the same one. @AndyRay– rowmna
Nov 8 at 17:05
Sorry, that was not clear enough. I am storing the ref to state based on the
onMouseEnter
then I am using that this.state.ref
to copy as text. That is the incorrect text though, always grabs the same one. @AndyRay– rowmna
Nov 8 at 17:05
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You always overwrite the same ref in your loop:
ref=this.textRef
So the last loop iteration will overwrite the ref with the last element, and all click handlers will reference that.
You should create an individual ref for each element. There's some suggestions for how to do this on this Github issue.
thank you this is very very insightful and helpful!
– rowmna
Nov 8 at 17:15
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You always overwrite the same ref in your loop:
ref=this.textRef
So the last loop iteration will overwrite the ref with the last element, and all click handlers will reference that.
You should create an individual ref for each element. There's some suggestions for how to do this on this Github issue.
thank you this is very very insightful and helpful!
– rowmna
Nov 8 at 17:15
add a comment |
up vote
1
down vote
accepted
You always overwrite the same ref in your loop:
ref=this.textRef
So the last loop iteration will overwrite the ref with the last element, and all click handlers will reference that.
You should create an individual ref for each element. There's some suggestions for how to do this on this Github issue.
thank you this is very very insightful and helpful!
– rowmna
Nov 8 at 17:15
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You always overwrite the same ref in your loop:
ref=this.textRef
So the last loop iteration will overwrite the ref with the last element, and all click handlers will reference that.
You should create an individual ref for each element. There's some suggestions for how to do this on this Github issue.
You always overwrite the same ref in your loop:
ref=this.textRef
So the last loop iteration will overwrite the ref with the last element, and all click handlers will reference that.
You should create an individual ref for each element. There's some suggestions for how to do this on this Github issue.
answered Nov 8 at 17:10
Andy Ray
16.7k66096
16.7k66096
thank you this is very very insightful and helpful!
– rowmna
Nov 8 at 17:15
add a comment |
thank you this is very very insightful and helpful!
– rowmna
Nov 8 at 17:15
thank you this is very very insightful and helpful!
– rowmna
Nov 8 at 17:15
thank you this is very very insightful and helpful!
– rowmna
Nov 8 at 17:15
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212440%2fref-not-referencing-correct-item-in-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is
ref
? you're setting the copy text to that every time– Andy Ray
Nov 8 at 16:59
Sorry, that was not clear enough. I am storing the ref to state based on the
onMouseEnter
then I am using thatthis.state.ref
to copy as text. That is the incorrect text though, always grabs the same one. @AndyRay– rowmna
Nov 8 at 17:05