将 ReactforwardRef 与 Redux connect 结合使用

2024-02-17

我有一个正在使用的 React 功能组件forwardRef像这样:

const wrapper = React.forwardRef((props, ref) => (
  <MyComponent {...props} innerRef={ref} />
));

export default wrapper;

现在我想使用以下方法将一些状态传递给该组件mapStateToProps与 Redux 的connect像这样的函数:

export default connect(mapStateToProps, null)(MyComponent);

我尝试通过以下方式将它们组合起来,但没有成功:

const wrapper = React.forwardRef((props, ref) => (
  <MyComponent {...props} innerRef={ref} />
));

export default connect(mapStateToProps, null)(wrapper);

我如何将这两者结合起来以获得所需的行为?


您可以使用options https://react-redux.js.org/api/connect#options-object在连接功能中。

connect(mapStateToProps, null, null, { forwardRef: true })(wrapper)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 ReactforwardRef 与 Redux connect 结合使用 的相关文章

随机推荐