【问题标题】:How do I export multiple function calls in react?如何在反应中导出多个函数调用?
【发布时间】:2019-10-12 06:23:32
【问题描述】:

我的文件中有这个:

 export default withAuth(authOptions)(ProfilePage);

但我还需要导出这个:

function mapStateToProps (state) {
  const { isLoggedIn } = state
  return { isLoggedIn}
}
const mapDispatchToProps = dispatch =>
  bindActionCreators({ logInUser }, dispatch)

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(ProfilePage)

我可以将它们结合起来,让它们都起作用吗?

【问题讨论】:

  • 为什么不这样呢? export default withAuth(authOptions)(connect( mapStateToProps, mapDispatchToProps )(ProfilePage));
  • 很高兴它有帮助!这就是为什么它起作用的原因,因为在 withAuth(authOptions)(SOME-COMPONENT) 中我们需要提供组件,而且 connect()() 返回一个将其替换为 SOME-COMPONENT 的组件解决了我们的问题。

标签: reactjs react-redux es6-modules module.exports


【解决方案1】:

您可以使用JS#named export

export const  mapStateToProps =  (state)=> {
  const { isLoggedIn } = state
  return { isLoggedIn}
}
export const mapDispatchToProps = dispatch =>bindActionCreators({ logInUser }, dispatch)

现在将其导入其他文件

//test.js

import ProfilePage,{mapStateToProps ,mapDispatchToProps} from "path_to_file"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2022-06-24
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    相关资源
    最近更新 更多