umijs/hox

How initialize a model only when used (lazy)

cardmaster opened this issue · 1 comments

e.g. we had a custom hook needs fetch data from network.

function someModel() {
  let [state, setState] = useState({loading:false, valid:false})
  useEffect(()=>{
     setState({loading:true, valid:false})
      axios().then(()=>{
         setState({loading:false, valid:true})         
     })
  }, []);
  return state;
}

export default createModel(someModel); //the network request will happens here.

The useEffect() will happen when using 'createModel', what should I do If I want the request happens only when I using that model?

e.g.

import useSomeModel ..

function SomeComponent() {
  let model = useSomeModel(); //I want the request happens on the first time using this model
}

hox 本身是不支持懒加载的,但是可以考虑用 useRequest手动触发