Passing function via props from Parent to Child component in React?

Emily Downey

I am having an issue where I'm trying to pass a function(updateEvents) via props from my App.js file to a NumberOfEvents.js file. I passed the same function to another component with no issues. However, when I try on the NumberOfEvents file, I get the following error: Error image Please help!!!

Here is the Parent:

import React, { Component } from 'react';
import EventList from './EventList';
import CitySearch from './CitySearch';
import NumberOfEvents from './NumberOfEvents';
import { extractLocations, getEvents } from './api';

import './nprogress.css';
import './App.css';

class App extends Component {
  state = {
    events: [],
    locations: [],
    numberOfEvents: 32
  }

  componentDidMount() {
    this.mounted = true;

    getEvents().then((events) => {
      if (this.mounted) {
        this.setState({
          events: events.slice(0, this.state.numberOfEvents),
          locations: extractLocations(events)
        });
      }
    });
  }

  componentWillUnmount() {
    this.mounted = false;
  }

  updateEvents = (location, eventCount) => {
    this.mounted = true;
    getEvents().then((events) => {
      const locationEvents = (location === 'all')
        ? events
        : events.filter((event) => event.location === location);
      this.setState({
        events: locationEvents,
        numberOfEvents: eventCount,
      });
    });
  };

  render() {
    return (
      <div className="App">
        <CitySearch
          locations={this.state.locations} updateEvents={this.updateEvents} />
        <EventList
          events={this.state.events} />
        <NumberOfEvents
          numberOfEvents={this.state.numberOfEvents}
          updateEvents={this.updateEvents} />
      </div>
    );
  }
}

export default App;

And here is the Child:

import React, { Component } from 'react';

class NumberOfEvents extends Component {
  state = {
    numberOfEvents: 32
  }

  handleChange = (event) => {
    const value = event.target.value;
    this.setState({
      numberOfEvents: value,
    });
    this.props.updateEvents('', value);
  };

  render() {
    return (
      <input
        className="number"
        value={this.state.numberOfEvents}
        onChange={this.handleChange} />
    )
  }
}

export default NumberOfEvents;

igmani

Im not sure this will help ...In Your Parent Component , inside return statement when passing the updateEvents Prop, try passing it as arrow function like this ....

updateEvents={ () => this.updateEvents() } />

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Closing react-bootstrap popup modal by passing function from parent component to child component via props not updating state

passing data from child to parent component - react - via callback function

passing props from parent to child in react class based component

passing props form child to parent component in react

Passing Function Props in React-Typescript from parent to child

Passing props from parent component to child components

React: How to pass function as props from Functional Parent component to child

React: Props undefined when passed from Parent to Child Function Component

Passing event and props from child to parent in react

React passing props from const to child component

Passing child component class as props to parent component in React

React router dom passing data from parent component to child router component does not pass the props.match

React, How to send props from parent component to child component? (to use those props outside render function )

passing a callback function via props to child component is undefined in child vue

Passing writable store values from parent to child component via props in Svelte

Passing props from parent component to child component on dialog box vue

Passing data from child to parent component in React

Passing state from parent component to child function

How can I pass image url from parent component to child component via props [REACT.JS]

React: How can you access the class name of a parent component from a child without passing it down as props?

Props is not passing in child component in react

Passing props to a Function as a Child component

Passing Props to Child component from Parent using cloneElement

Vue parent component props are not passing to child component

Add props from parent component in map() function from child component

Passing a function from a react component to its parent then from the parent to another child

React Passing Props from parent to child behaves unexpectedly

react native - Passing navigation props from parent to child

Passing data from parent class to child function -props undefined