Truncating text with onReceive #839
-
In the various Concise Forms case studies there is an action that forces the displayName to always be 16 characters or less. This succeeds as far as the state variable displayName is concerned, but the TextField shows every character typed even though the variable itself is only 16 characters long. If one isn't using TCA you could get the TextField to mirror the state (i.e. Never display more than 16 characters) like this:
I don't understand how to achieve the same behavior when using TCA. I've tried this:
Which results in onReceive never being called And this:
Which also results in onReceive never being called. I'm sure I just don't understand the TCA well enough to figure this out. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @jharvey3, I'd say use So your example would look something like this // Reducer
...
case .truncateUserName:
state.userName = String(state.userName.prefix(16))
return .none
...
// View
...
TextField(
"User Name",
text: viewStore.binding(
get: \.userName,
send: TruncateTextAction.userNameChanged
)
)
.onChange(of: viewStore.userName) { _ in viewStore.send(.truncateUserName) }
... Here is a discussion (#761) that goes into it a little bit more with some other options as well. |
Beta Was this translation helpful? Give feedback.
Hey @jharvey3,
I'd say use
.onChange
instead of.onReceive
and add an action responsible for truncating the userNameSo your example would look something like this
Here is a discussion (#761) that goes into it a little bit more with some other options as well.