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

时间:2019-12-18 17:24:24

标签: reactjs

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

我想在另一个类中调用onToggleOpen,反正有做吗?

1 个答案:

答案 0 :(得分:0)

您可以将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 };