为什么子组件比父组件有更多的道具?

Why does the child component have more props than the parent?

我有一个父组件,我想成为多个子组件的选项卡。出于某种原因,我的子组件比父组件有更多的道具数据。

父组件

import React, { Component} from 'react';
import { Link } from 'react-router';
import { connect } from 'react-redux';

class TerritoryTabs extends Component {

  componentWillMount() {
    console.log('this is the parent props (tabs)')
    console.log(this.props);
  }

  render() {
    return (

    {this.props.children}

    );
  }

}

export default connect(null, null)(TerritoryTabs);

子组件

import React, { Component} from 'react';
import { Link } from 'react-router';
import { connect } from 'react-redux';
import { reduxForm } from 'redux-form';

import { getTerritoryGeographies } from '../actions/index';

import TerritoryTabs from './territory-tabs';

class TerritoryGeographyList extends Component {

  componentWillMount() {
    console.log('this is child props (TerritoryGeographyList)');
    console.log(this.props);
    this.props.getTerritoryGeographies(this.props.params.id);
  }

  render() {
    return (
    <TerritoryTabs>

          <div>This list goes here</div>

    </TerritoryTabs>
    );
  }
}

function mapStateToProps(state) {
  return { territoryGeographies: state.territoryGeographies.all
        };
}

export default connect(mapStateToProps, { getTerritoryGeographies })(TerritoryGeographyList);

这是控制台打印的内容。

包装组件应称为 "parent"。示例:

<Parent>
    <Child />
</Parent>

所以,在你的情况下,修正这一行:

class TerritoryTabs extends Component {

  componentWillMount() {
    console.log('this is the child props (tabs)') // <-- Fix this line, it should be the "child"
    console.log(this.props);
  }

这行:

class TerritoryGeographyList extends Component {

  componentWillMount() {
    console.log('this is parent props (TerritoryGeographyList)'); // <-- fix this line, it should be "parent"
    console.log(this.props);
    this.props.getTerritoryGeographies(this.props.params.id);
  }