Skip to content

Truncating text with onReceive #839

Answered by iampatbrown
jharvey3 asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @jharvey3,

I'd say use .onChange instead of .onReceive and add an action responsible for truncating the userName

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.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jharvey3
Comment options

Answer selected by jharvey3
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