繁体   English   中英

有没有办法在其他组件中使用 withStateHandlers 中的函数?

[英]Is there a way to use a function in withStateHandlers in other component?

const Map = compose(
    withStateHandlers(() => ({
        isOpen: false,
        markerIndex: 0
    }), {
        onToggleOpen: ({ isOpen }) => (index) => ({
            isOpen: true,
            markerIndex: index
        })
    }),
    withScriptjs,
    withGoogleMap
)

我想在另一个类中调用 onToggleOpen ,有办法吗?

您可以将onToggleOpen提取到一个函数中,然后将其导出以在另一个文件中使用。

const onToggleOpen = ({ isOpen }) => index => ({
  isOpen: true,
  markerIndex: index
});

const Map = compose(
  withStateHandlers(() => ({ isOpen: false, markerIndex: 0 }), {
    onToggleOpen
  }),
  withScriptjs,
  withGoogleMap
);

// ES6 module syntax
export { onToggleOpen };
// CJS syntax
modules.exports = { onToggleOpen };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM