5

I want autocorrect. However, when the user types "@", I want autoCorrect to turn off until otherwise.

I have tried setting the prop to false/true. However, the component does not change the setting (while there is text in the TextInput).

How can I get around this?

(iOS only)

4
  • maybe this answer can help you. stackoverflow.com/questions/23453430/… Commented May 5, 2019 at 4:54
  • but that's for android Commented May 5, 2019 at 7:19
  • 1
    Can you show some of your code ? Might help us get a better understanding of how this can be fixed :) Commented May 7, 2019 at 10:05
  • @TIMEX, you asked this question before in this link, The answer to that question is the same as this question post. how to deal with the solution. Do you find any way to disable animation? why you didn't accept that answer for the issue, because that is the answer this currently accepted answer Commented Aug 13, 2019 at 11:18

1 Answer 1

13
+500

Demos

demo gifdemo2

Code

checkTest function

See code comments for the most important remarks.

checkText(text){
      //create a new regular expression 
      const regex = new RegExp("@");
      //check if the string contains an @ 
      const res = text.match(regex);

      // if res is not null, we have a match! 
      if (res != null){
        if (this.state.autoCorrect){
          // disable auto correction if it's still enabled
          this.input.blur(); 
          // hacky part, we need to dismiss the keyboard first, then we can show it again. 
          this.setState({autoCorrect: false}, () => {
            setTimeout(() => this.input.focus(), 60);
          });
        }
      }else{
        if (!this.state.autoCorrect){
          this.input.blur(); 
          // enable auto correction if no @ is detected anymore
          this.setState({autoCorrect: true}, () => {
            setTimeout(() => this.input.focus(), 60);
          });
        }
      }
      //update text in state 
      this.setState({ username: text});
    }

render function

 <View style={styles.container}>
     <TextInput 
      value={this.state.username}
      onChangeText={text => this.checkText(text)}
      autoCorrect={this.state.autoCorrect}
      />
 </View>

Complete Working Example

https://snack.expo.io/Skta6BJ34

Discussion

It seems, you need to "reload" the Keyboard to affect the reloading of the autoCorrect property. I think this is still a bug and is hopefully resolved in a future release. (see this github issue).

In the meanwhile, you can use this little workaround and maybe do some fine tuning on the timings/regex etc.

Edit:

I found an extensive answer here, this one tackles the issue similarly.

Sign up to request clarification or add additional context in comments.

4 Comments

How do I disable the keyboard animation while this is happening? In Swift, there is a way to do so (setAnimationsEnabled) ...is there a way in RN?
@TIMEX Unfortunately, react native does not support setting setAnimationsEnabled at the moment.
Btw I think the animation is not so annoying, it actually shows the user that now something(the auto correction) is different. But that's just my opinion.
The reload part of the app was my problem. Still in 2023, you have to reload your app for the keyboard changes to take effects. When passing autoCorrect to false or true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.