使用带有mapStateToProps的ref不会起作用反应

时间:2017-08-03 12:38:09

标签: reactjs react-redux

我尝试用ref调用子函数,但是子函数有一个reduce函数,所以它确实有效:

我的父母:

search(data) {
    this.SearchProduct.search(data);
}
...
<SearchProduct ref={instance => { this.SearchProduct = instance; }} />

我的孩子:

class SearchProduct extends React.Component {
...

    search(data) {
       console.log(data)
    }
...
}

const mapStateToProps = (state, ownProps) => ({
    basket: state.basket,
});


const mapDispatchToProps = (dispatch, ownProps) => ({

    setBasket: (basket) => {
        dispatch({ type: 'SET_BASKET', basket });
    }
});

module.exports = connect(
    mapStateToProps,
    mapDispatchToProps,
    null,
    { withRef: true }
)(SearchProduct);

当我解雇时:

component.js:25 Uncaught TypeError: this.SearchProduct.search is not a function

任何人都有想法?

非常感谢

1 个答案:

答案 0 :(得分:0)

你需要得到这样的包装实例

\

Docs