【问题标题】:Passing function via props from Parent to Child component in React?在 React 中通过 props 从父组件传递函数到子组件?
【发布时间】:2021-10-20 17:49:16
【问题描述】:

我遇到了一个问题,我试图通过道具将函数(updateEvents)从我的 App.js 文件传递​​到 NumberOfEvents.js 文件。我将相同的功能传递给另一个组件,没有任何问题。但是,当我尝试 NumberOfEvents 文件时,出现以下错误: Error image 请帮忙!!!

这里是父级:

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;

这是孩子:

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;

【问题讨论】:

  • 如果您在孩子中控制台记录 this.props,您会看到什么?
  • @richardsefton 它只是控制台记录一个空对象{}

标签: javascript reactjs class components


【解决方案1】:

尝试向子组件添加构造函数

constructor(props) {
    super(props);
    this.state = {
        numberOfEvents: 32   
    }
}

【讨论】:

  • 当我尝试这样做时,它仍然给我同样的错误,但控制台将 this.props 记录为:{ numberOfEvents: 32, updateEvents: [Function (anonymous)] }
  • 但看起来 { numberOfEvents: 32, updateEvents: [Function (anonymous)] } 的日志记录发生在 App.js 中,它在 NumberOfEvents 中仍然是一个空对象:(
【解决方案2】:

我不确定这会有所帮助...在您的父组件中,在传递 updateEvents Prop 时在 return 语句中,尝试将其作为箭头函数传递......

updateEvents={ () =&gt; this.updateEvents() } /&gt;

【讨论】:

  • 我也试过了!还是同样的错误。 ://
  • 在 this.updateEvents( ) 中传递必要的参数
  • 这似乎有所作为!谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
  • 2018-12-28
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
相关资源
最近更新 更多