我有一个更新我的应用程序的通知状态的操作.通常,此通知将是某种错误或信息.我需要在5秒后发送另一个操作,将通知状态返回到初始状态,因此不会发出通知.这背后的主要原因是提供通知在5秒后自动消失的功能.
我没有运气使用setTimeout
和返回另一个动作,也无法找到如何在线完成.所以欢迎任何建议.
在React中,状态不会立即更新,因此我们可以使用回调setState(state, callback)
.但是如何在Redux中做到这一点?
在调用之后this.props.dispatch(updateState(key, value))
,我需要立即对更新后的状态做一些事情.
有什么方法可以用最新状态调用回调,就像我在React中做的那样?
在它周围,但不是因为我有足够的想法redux-thunk
,react-router
但我没有得到这个看似简单的想法:
调用路线的改变通过编程<Route/>
的history.push('/')
后一个动作完成分发到店里,当按钮被按下这样的事情发生.
const LoggerRedux = ({stateProp, LogIn}) => {
return(
<div>
<h2>Here is the button. Use this to login</h2>
<Route render={({ history}) => (
<button
type='button'
onClick={
//Something that calls {LogIn}
//and then the history.push('/')
}
>
Log yo Arse Inn
</button>
)}>
</Route>
</div>
)
}
const mapStateToProps = (state) => {
return {
stateProp : state
}
}
const mapDispatchToProps = (dispatch) => {
return {
LogIn : () => …
Run Code Online (Sandbox Code Playgroud) 因为它保证按时间store.dispatch返回,状态已经改变了?如果是这种情况,那么为什么不通过调度调用返回newState?
store.dispatch( action1() );
store.dispatch( action2() );
Run Code Online (Sandbox Code Playgroud)
一个示例,我将在action1中登录用户,然后我要触发另一个将使用LOGEDIN用户信息来进一步更改状态的操作。因此,我想确保除非action1已成功更改状态,否则不会触发action2。
因此,在store.dispatch返回时,是否保证状态已经改变?
减速器示例:
function reducer(state, action){
// please ignore that i will mutate state, just for sake of simplicity of example.
if(action.type==='ACTION1'){
state.user_id = action.payload;
return state;
}
if(action.type==='ACTION1'){
state.value2 = state.user_id * action.whatEver;
return state;
}
return state;
}
Run Code Online (Sandbox Code Playgroud)
我当前的保护是我使用React.component监视对user_id的更改,然后触发action2,但是如果Redux Actions是同步的,则可以在action1之后直接触发action2并减少我的样板。
我知道redux中的调度操作是同步的.当我从第1行的反应组件向redux发送操作时,我可以100%确定反应组件中的第2行是否具有更新的redux状态?我正在通过调度getGameDataSuccess(data)
动作设置游戏数据.我可以100%确定在下一行中新的道具会流下来吗?现在当我console.log(this.props.lostLetters.gameData)
,我看到新的游戏数据.但我能100%确定这种情况总是如此吗?
getLostLettersGame(this.props.gameCenter.selectedGame.gameId)
.then((data) => {
this.props.dispatch(LostLettersActions.getGameDataSuccess(data));
console.log(this.props.lostLetters.gameData);
this.generateGame();
})
Run Code Online (Sandbox Code Playgroud) redux ×5
react-redux ×3
reactjs ×3
javascript ×2
flux ×1
react-router ×1
redux-thunk ×1
timeout ×1