Filter is a hook before a component enters(or leaves). React-Keeper supports two filters: Enter Filter
and Leave Filter
.
Enter filters
, filters run before a route mount succeed, such as : login's check.
Leave filters
, filters run before a route unmount succeed, such as : unsubmited form data.
- Define a fllter, and run over it or not.
// receive 'props' from React-Control
const loginFilter = (callback, props)=> {
if(!props.host) {
// dynamicly request data (use jQuery ajax)
$.ajax({
url: 'host/login.do',
data: {},
succeed: function(data){
if(data.host){
// run 'callback' function to enter next step (render component or next filter)
callback()
}
},
error: function(){
},
dataType: 'json'
})
}
}
- Single Filter
<HashRouter>
<Route path='/user' component={User} enterFilter={ loginFilter } leaveFilter={(cb)=>{ alert('will not leave') }} />
</HashRouter>
- Multiple Filters
<HashRouter>
<Route path='/user' component={User} enterFilter={[ loginFilter, permitFilter1, permitFilter2 ] } />
</HashRouter>