starfx
v0.13.4
React应用程序的Micro-MVC框架。
如果我们用MVC来考虑react是视图,则starfx是模型和控制器。
开始
特征:
import { createApi , createSchema , createStore , mdw , timer } from "starfx" ;
import { Provider , useCache } from "starfx/react" ;
const [ schema , initialState ] = createSchema ( ) ;
const store = createStore ( { initialState } ) ;
const api = createApi ( ) ;
// mdw = middleware
api . use ( mdw . api ( { schema } ) ) ;
api . use ( api . routes ( ) ) ;
api . use ( mdw . fetch ( { baseUrl : "https://api.github.com" } ) ) ;
const fetchRepo = api . get (
"/repos/neurosnap/starfx" ,
{ supervisor : timer ( ) } ,
api . cache ( ) ,
) ;
store . run ( api . bootup ) ;
function App ( ) {
return (
< Provider schema = { schema } store = { store } >
< Example />
</ Provider >
) ;
}
function Example ( ) {
const { isInitialLoading , isError , message , data } = useCache ( fetchRepo ( ) ) ;
if ( isInitialLoading ) return "Loading ..." ;
if ( isError ) return `An error has occurred: ${ message } ` ;
return (
< div >
< h1 > { data . name } </ h1 >
< p > { data . description } </ p >
< strong > ? { data . subscribers_count } </ strong > { " " }
< strong > { data . stargazers_count } </ strong > { " " }
< strong > ? { data . forks_count } </ strong >
</ div >
) ;
}