Skip to content

@FocusState: focusedField binding not triggering the binding action when the focusedField is set from the Reducer #2946

Answered by mbrandonw
pdrcabrod asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @pdrcabrod, yeah this is to be expected. If the focus changes directly in a reducer from some other action:

case .buttonTapped:
  state.focusedField = .title

…then the .binding action can't possibly be destructured in the switch. After all, a .binding action wasn't sent, the .buttonTapped action was sent.

If you just want to listen for any changes to focusField you can use the onChange reducer operator:

CombineReducers {
  BindingReducer()
  Reduce { state, action in  }
}
.onChange(of: \.focusField) {  }

You can look at the docs for onChange for more information of how to use it.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by pdrcabrod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants