我的路线定义如下:
<Route component={App}>
<IndexRoute component={Main}/>
<Route path="/foo" component={Foo}/>
<Route path="/bar" component={Bar}/>
</Route>
Run Code Online (Sandbox Code Playgroud)
默认情况下,组件当您从过渡获得卸载Foo到Bar。由于我在组件Foo(带有自定义动画的谷歌地图)中有一个计算量很大的代码,我想阻止卸载和隐藏这个组件,所以当用户返回时它会立即加载。
如何实现这一目标?
我有很多组件,导入部分看起来很臃肿:
import {comp1} from './components/comp1/comp1';
import {comp2} from './components/comp2/comp2';
....
Run Code Online (Sandbox Code Playgroud)
有没有办法写通用导入?就像是
import * from './components/';
Run Code Online (Sandbox Code Playgroud) Puppeteer 可以将 sessionStorage 和 localStorage 中的值存储在磁盘上并下次使用吗?
Mapbox GL提供了一种添加自定义标记的方法,但它需要HTMLElement作为参数,强制使用ReactDom首先渲染标记,然后追加它:
const placeholder = document.createElement('div');
const marker = ReactDOM.render(
<div className='mapboxgl-marker'>
<marker/>
</div>, placeholder);
const markerRef = new mapboxgl.Marker(marker).setLngLat(position).addTo(map);
Run Code Online (Sandbox Code Playgroud)
我认为这是次优解决方案,因为它发生在render()函数之外.
有更好的方法吗?
我有解析json配置的代码:
import (
"encoding/json"
"os"
"fmt"
)
type Configuration struct {
Users []string
Groups []string
}
type AnotherConfiguration struct {
Names []string
}
file, _ := os.Open("conf.json")
decoder := json.NewDecoder(file)
configuration := Configuration{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(configuration.Users)
Run Code Online (Sandbox Code Playgroud)
如您所见,我有两种不同类型的 Configuration 和 AnotherConfiguration。
我不太清楚如何创建一个通用函数,它会返回任何类型(配置或另一个配置)的配置。
像这样的东西:
func make(typename) {
file, _ := os.Open("conf.json")
decoder := json.NewDecoder(file)
configuration := typename{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("error:", err)
}
return configuration
}
Run Code Online (Sandbox Code Playgroud)