将两个道具从父组件传递到子组件会导致在父组件中未定义

时间:2018-01-15 13:13:52

标签: javascript reactjs components react-props

在我的react redux项目中,我试图将子组件中的props传递给父组件。更具体地说,我想将两条信息:product和counter传递给父容器,其中两条信息都打算在函数内部使用,如下所示:

addProductToBasket(product, counter) {

}

我的问题是:在addProductToBasket函数中无法识别计数器,我收到此错误消息:

  

未捕获的ReferenceError:未定义计数器

为什么这里没有定义计数器,如何在此功能中正确使用它?

以下是相关的父容器代码:

class Products extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            listOn: false,
            total: 0,
        }

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

    addProductToBasket(product, counter) {
        var price = Number(product.price);
        console.log(price);
        this.setState(prevState => ({
            total: prevState.total + price,
        }), 
        () => {
            var total = this.state.total
            console.log(counter);
            this.props.updateBasket(total, price, counter, product);
        })  
    }

    render() {
        const grid = this.props.products.map((product, i) => {
            return (
                <GridLayout
                    key={i}
                    name={product.name}
                    id={product.id}
                    description={product.description}
                    price={product.price}
                    addProductToBasket={() => this.addProductToBasket(product, counter)}
                />
            );
        });

        return(
            <Grid>
                <Row>
                      {grid}
                </Row>
            </Grid>
        );
    }
}

function mapStateToProps(state) {
    return {
        products: state.products
    };
}

function mapDispatchToProps(dispatch) {
    return bindActionCreators({ updateBasket: updateBasket }, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(Products);

和子组件代码:

export default class GridLayout extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            counter: 0,
        }

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

    handleAdd(product) {
        this.setState({counter: this.state.counter + 1}, () => {
            var counter = this.state.counter
            console.log('counter is' + counter)
            this.props.addProductToBasket(product, counter);
        });  
    }

    render() {
        return(
            <Col xs={12} md={4}>
                <div style={{padding: '10px', backgroundColor: 'white', height: '120px', width: '100%'}}>
                    <div>{this.props.id}</div>
                    <div>{this.props.name}</div>
                    <div>{this.props.description}</div>
                    <div>$ {this.props.price}</div>
                    <div style={{float: 'right', cursor: 'pointer'}} onClick={this.handleAdd}>
                        +
                    </div>
                </div>
            </Col>
        );
    }
}

2 个答案:

答案 0 :(得分:0)

这是因为您忘记在箭头函数中接收从父组件内的子项传递的参数。

下面:

addProductToBasket={() => this.addProductToBasket(product, counter)}

使用此:

addProductToBasket={(prodt, counter) => this.addProductToBasket(prodt, counter)}

原因是:您没有将addProductToBasket直接传递给孩子,您正在传递 arrow function ,并且在箭头功能体内您正在调用addProductToBasket,因此您需要在箭头函数中接收参数并将这些参数传递给addProductToBasket。

其他可能的解决方案是:

删除箭头功能并按如下方式编写:

addProductToBasket={ this.addProductToBasket }

答案 1 :(得分:0)

替换此行:

addProductToBasket={() => this.addProductToBasket(product, counter)}

有了这个:

addProductToBasket={this.addProductToBasket.bind(this, product, counter)}