Skip to content
Advertisement

i am failing to access a func in my child component

the are about three more components between this one and the one i want it to function in, so i keep on passing it down as a prop till i get to the component i want. it is supposed to run when the user clicks a button but i get an error that this.props.route.params.ToggleFavorites is not a function please help!!!!

JavaScript

Advertisement

Answer

If you’re passing it down as a prop, it won’t be in the route params, it will just be in the props at the top level (i.e. this.props.ToggleFavorites). The route params are only populated by navigation (i.e. when you call navigation.navigate('screen', {data: 'hi'}), you’ll get {data: 'hi'} in this.props.route.params).

Instead of passing the function down so many levels, consider using composition instead. When used properly, this pattern will eliminate boilerplate and reduce error surface area. https://plainenglish.io/blog/how-to-avoid-prop-drilling-in-react-using-component-composition

Another option is to store the function in a Context, although that has more caveats and should be used with care. https://reactjs.org/docs/context.html

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement