[ad_1]
I am making an attempt to seize the textual content typed in a TextEditor, retailer it in a variable and replace what’s displayed on the TextEditor itself. I am utilizing the .onChange() to set off and seize what’s being typed, retailer it in a variable, and alter the present textual content being displayed. However the issue is that, .onChange() is being referred to as twice: First, when i kind the textual content contained in the TextEditor; and Second, when i attempt to set a special character for the view itself.
struct ChatView: View {
@State var chatTextInput: String = ""
@State var oldChatValue: String = ""
@State var newChatValue: String = ""
var physique: some View {
VStack {
TextEditor(textual content: $chatTextInput)
.onChange(of: chatTextInput) { newValue in
oldChatValue += "a"
newChatValue += newValue.final.map{String($0)}!
chatTextInput = oldChatValue
}
}
}
}
For exemple, if i kind qwerty, newChatValue is printed as Qawaearataya and chatTextInput receives aaaaaaaaaaaa
Any clues on the right way to forestall the .onChange to being triggered the second time, when i set the brand new character for the TextEditor textual content?
Thanks lots!
[ad_2]
