【发布时间】:2018-12-10 04:42:12
【问题描述】:
在我的项目中,我使用recompose/react-google-maps 定义了一个顶级组件,如下所示:
const LocationPicker = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,places&key=AIzaSyDnKwHUG_EJXN5EEW6hTftZHYo8E8QTTXY",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withScriptjs,
withGoogleMap,
withState('places', '', ''),
withHandlers(() => {
refs = {
map: undefined,
}
return {
onMapMounted: () => ref => {
refs.map = ref
service = new google.maps.places.PlacesService(refs.map.context.__SECRET_MAP_DO_NOT_USE_OR_YOU_WILL_BE_FIRED);
},
}
}),
)((props) => {
return (
<GoogleMap
ref={props.onMapMounted}
defaultZoom={13}
defaultCenter={{ lat: 42.3570637, lng: -71.06257679999999 }}
>
{props.marker &&
<Marker position={{ lat: props.marker.lat, lng: props.marker.lng }} draggable={true} />
}
</GoogleMap>
)
})
这是一个定义在类范围之外的组件。我收到以下错误:
TypeError: Object(...) is not a function
为什么我不能这样定义组件?
【问题讨论】:
-
如何导入
compose函数?您是否将其导入为import { compose } from 'recompose'? -
是的 - 我正在从 recompose 导入 compose
-
抱歉,我的问题不清楚。您是否在导入时将
compose包裹在花括号中?我很确定重组需要花括号。它应该是import { compose } from 'recompose'而不是import compose from 'recompose'。这是两个不同的导入,如果花括号丢失,我可以看到您发布的错误消息。 -
我使用花括号按我应该的方式导入(例如
import { compose } from 'recompose')。 -
你试过一步步删除
compose中的函数吗?
标签: reactjs react-google-maps recompose