【发布时间】:2023-03-30 23:35:02
【问题描述】:
尝试使用 ref 从其父级调用组件的函数。该组件连接到 Redux。使用反应 ^16。说:TypeError: this.usersTable.getWrappedInstance is not a function。请参阅下面的代码示例:
export default class Users extends Component {
constructor(props){
super(props)
this.usersTable = React.createRef()
}
render() {
return (
<div>
<button type="button"
onClick={this.usersTable.getWrappedInstance().onAddUser}>ADD NEW USER</button>
<UsersTable onRef={node => this.usersTable = node} />
</div>
)
}
}
class UsersTable extends Component {
componentDidMount() {
this.props.onRef(this)
}
onAddUser = () => {
// DO SMTH
}
}
export default connect(mapStateToProps, mapDispatchToProps, null, { withRef: true })(UsersTable)
【问题讨论】: