Javascript 将React类组件迁移到函数组件(React google maps)
我正在使用Javascript 将React类组件迁移到函数组件(React google maps),javascript,reactjs,Javascript,Reactjs,我正在使用react google maps库进行一个项目,我找到了一个完美的例子来满足我的需求。它是下面的代码,正如您所看到的,它是一个类组件 class Map extends Component { constructor(props){ super(props); this.map = React.createRef(); } render() { const MyGoogleMap = withScriptjs( withGoogleM
react google maps
库进行一个项目,我找到了一个完美的例子来满足我的需求。它是下面的代码,正如您所看到的,它是一个类组件
class Map extends Component {
constructor(props){
super(props);
this.map = React.createRef();
}
render() {
const MyGoogleMap = withScriptjs(
withGoogleMap(props => (
<GoogleMap
ref={map => {
this.map = map;
}}
/>
))
);
return(
<MyGoogleMap />
);
}
}
export default Map
类映射扩展组件{
建造师(道具){
超级(道具);
this.map=React.createRef();
}
render(){
const MyGoogleMap=withScriptjs(
使用谷歌地图(道具=>(
{
this.map=map;
}}
/>
))
);
返回(
);
}
}
导出默认映射
我想在函数组件中迁移这个类
function Map() {
const mapRef = useRef();
const MyGoogleMap = withScriptjs(
withGoogleMap(props => <GoogleMap ref={mapRef} />)
);
return <MyGoogleMap />;
}
我不知道如何实现函数组件的行是
this.map=React.createRef()代码>
及
ref={map=>{this.map=map}}
在a中,使用钩子
useRef
和createRef
之间有一个细微的区别:请参阅本文。
确保在函数组件中清除此。
function Map() {
const mapRef = useRef();
const MyGoogleMap = withScriptjs(
withGoogleMap(props => <GoogleMap ref={mapRef} />)
);
return <MyGoogleMap />;
}
函数映射(){
const mapRef=useRef();
const MyGoogleMap=withScriptjs(
使用谷歌地图(道具=>)
);
返回;
}
在一个游戏中,你使用一个钩子
useRef
和createRef
之间有一个细微的区别:请参阅本文。确保在函数组件中清除此。
function Map() {
const mapRef = useRef();
const MyGoogleMap = withScriptjs(
withGoogleMap(props => <GoogleMap ref={mapRef} />)
);
return <MyGoogleMap />;
}
函数映射(){
const mapRef=useRef();
const MyGoogleMap=withScriptjs(
使用谷歌地图(道具=>)
);
返回;
}
这可能会有帮助这可能会有帮助