简体   繁体   English

this.props不是从父组件到子组件的函数还原

[英]this.props is not a function redux from parent to child component

Hi I am having an issue calling an action in a child component. 嗨,我在调用子组件中的操作时遇到问题。

However, I can run the CompanyForm component by itself and it'll work, but when treated as a child component, I run into an error: 但是,我可以单独运行CompanyForm组件,并且可以运行,但是当被视为子组件时,我会遇到错误:

Uncaught TypeError: this.props.createCompany is not a function 未捕获的TypeError:this.props.createCompany不是函数

Companies.js Companies.js

render() {
    return (
        <div>
            <CompanyForm/>
        </div>
    );
}

CompanyForm.js CompanyForm.js

import { createCompany } from "../../actions/companyAction";
export class CompanyForm extends Component {
constructor(props) {
    super(props);

    this.state = {
        companyName: '',
        street1: '',
        street2: '',
        city: '',
        state: '',
        zipcode: ''
    };

    this.handleSubmit = this.handleSubmit.bind(this);
}


handleSubmit(event) {
    event.preventDefault();
    const company = {
        ...
    };
    console.log(company);
    this.props.createCompany(company)

}
}
export default connect(null, { createCompany })(CompanyForm);

companyActions.js companyActions.js

export const createCompany = (companyData) => dispatch => {
console.log('CREATE_COMPANY: ', companyData);
fetch('api/Company/Create', {
    ...
)};

import child components without curly braces into the parent component ie 将不带花括号的子组件导入父组件,即

import CompanyForm from '../component/companyForm/CompanyForm';

NOT

import { CompanyForm } from '../component/companyForm/CompanyForm';

You are exporting both the component and the connected component you have to make sure you are importing the right one. 您必须同时导出组件和连接的组件,以确保导入正确的组件。 The right one in this case is the connected component. 在这种情况下,正确的是连接的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM