I'm trying to use a value that is generated within a function in child.js and pass this to parent.js so I can use this value to filter the array of children.
Child.js
const returnDistance = () => {
const dist = getDistance(userLat, userLng, lat, lng).toFixed(1);
this.props.onCalculateDistance(dist);
return dist;
}
return dist
is the value that is generated that I need to access within parent.js
Parent.js
// The callback function
distanceChange = (dist) => {
console.log(dist)
return dist;
}
//The map to render child.js data. Im passing distanceChange as a prop.
{filteredResults.filter(({ node }) => (node.onCalculateDistance >= 0)).map(({ node }) => {
return (
<SpaceWrapper key={node.id}>
<Space node={node} onCalculateDistance={this.distanceChange} />
</SpaceWrapper>
)
}
)}
Am I missing something glaringly obvious here? The console.log for distanceChange
returns all of the values for the child.js nodes but returns undefined when used as a prop.
How do I get the function to run so I can filter the results with the returned value?
Thanks