I want to call SingleCard
child component methods in renderHiddenItem
. I have assigned different ref name for each renderItem
. But when I call this.name, it is undefined
. Anything is wrong in this code? How can I achieve this?
<SwipeListView
data={this.state.listViewData}
renderItem={(data, i) => {
const name = 'childRef'+i
return (
<SingleCard
ref={component => this.name = component}
itm={data.item}
/>
);
}}
renderHiddenItem={(data, i) => {
const name = 'childRef'+i
return (
<TouchableOpacity onPress={ () => console.log(this.name)}>
<Text> h </Text>
</TouchableOpacity>
);
}
}}
/>
Update:
I want to trigger some action which is written in the singleCard component. Need to call that in renderHiddenItem
.
Like this:
this.childRef0.someMethod
this.childRef1.someMethod
Instead of name you need to use the dynamic variable which can be done by using the bracket notation
Also when you use
ref
on a component which is created using an HOC for instanceconnect
fromreact-redux
, most of the libraries provide a method calledgetWrappedInstance
to get the ref for the actual component instead of theconnect
component. You can use it likebut initially you need to set
{withRef: true}
as the fourth parameter toconnect
being used inSingleCard
likeYou can read more about it here
https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options