Cannot turn autoCorrect to {false} in react native TextInput
up vote
10
down vote
favorite
On my TextInput change text, I detect whether the user pushed the @
button for mentions.
onChangeText(text){
const suggestTrigger = text.match(/B@[A-Za-z0-9]*$/i) //grab "@" trigger
const searchQuery = (suggestTrigger && suggestTrigger.length > 0) ? suggestTrigger[0] : null;
this.setState({
searchQuery: searchQuery
})
}
Then, in my render, I do:
<TextInput
autoCapitalize={this.state.searchQuery ? "none" : "sentences"}
autoCorrect={this.state.searchQuery ? false : true}
onChangeText={this.onChangeText}
/>
However, even when I do this, the autoCorrect does not turn off.
I still see this:
This is causing problems because oftentimes the system replaces the entire mention with a different word altogether.
How can I turn autoCorrect and autoCapitalize off when the user pushes the @
button?
'
I have even tried rendering an entirely different , but the behavior remains.
ios react-native text keyboard
This question had a bounty worth +500
reputation from TIMEX that ended 13 hours ago. Grace period ends in 10 hours
This question has not received enough attention.
|
show 3 more comments
up vote
10
down vote
favorite
On my TextInput change text, I detect whether the user pushed the @
button for mentions.
onChangeText(text){
const suggestTrigger = text.match(/B@[A-Za-z0-9]*$/i) //grab "@" trigger
const searchQuery = (suggestTrigger && suggestTrigger.length > 0) ? suggestTrigger[0] : null;
this.setState({
searchQuery: searchQuery
})
}
Then, in my render, I do:
<TextInput
autoCapitalize={this.state.searchQuery ? "none" : "sentences"}
autoCorrect={this.state.searchQuery ? false : true}
onChangeText={this.onChangeText}
/>
However, even when I do this, the autoCorrect does not turn off.
I still see this:
This is causing problems because oftentimes the system replaces the entire mention with a different word altogether.
How can I turn autoCorrect and autoCapitalize off when the user pushes the @
button?
'
I have even tried rendering an entirely different , but the behavior remains.
ios react-native text keyboard
This question had a bounty worth +500
reputation from TIMEX that ended 13 hours ago. Grace period ends in 10 hours
This question has not received enough attention.
This seems to work for me. snack.expo.io/B1PMIxzTm
– gtfargo
Nov 8 at 17:46
@gtfargo it may work on snack but it doesn't work on real device (exact code)
– TIMEX
Nov 8 at 20:02
which version of react-native are you using?
– lazreg87
Nov 13 at 18:21
Would you please upload a sample project?
– Mojtaba Hosseini
Nov 15 at 10:04
@MojtabaHosseini, Absolutely it is an issue fromiOS
native side and should be fixed from the native side of the project, but I add an innovative solution for solving this problem.
– AmerllicA
Nov 15 at 13:40
|
show 3 more comments
up vote
10
down vote
favorite
up vote
10
down vote
favorite
On my TextInput change text, I detect whether the user pushed the @
button for mentions.
onChangeText(text){
const suggestTrigger = text.match(/B@[A-Za-z0-9]*$/i) //grab "@" trigger
const searchQuery = (suggestTrigger && suggestTrigger.length > 0) ? suggestTrigger[0] : null;
this.setState({
searchQuery: searchQuery
})
}
Then, in my render, I do:
<TextInput
autoCapitalize={this.state.searchQuery ? "none" : "sentences"}
autoCorrect={this.state.searchQuery ? false : true}
onChangeText={this.onChangeText}
/>
However, even when I do this, the autoCorrect does not turn off.
I still see this:
This is causing problems because oftentimes the system replaces the entire mention with a different word altogether.
How can I turn autoCorrect and autoCapitalize off when the user pushes the @
button?
'
I have even tried rendering an entirely different , but the behavior remains.
ios react-native text keyboard
On my TextInput change text, I detect whether the user pushed the @
button for mentions.
onChangeText(text){
const suggestTrigger = text.match(/B@[A-Za-z0-9]*$/i) //grab "@" trigger
const searchQuery = (suggestTrigger && suggestTrigger.length > 0) ? suggestTrigger[0] : null;
this.setState({
searchQuery: searchQuery
})
}
Then, in my render, I do:
<TextInput
autoCapitalize={this.state.searchQuery ? "none" : "sentences"}
autoCorrect={this.state.searchQuery ? false : true}
onChangeText={this.onChangeText}
/>
However, even when I do this, the autoCorrect does not turn off.
I still see this:
This is causing problems because oftentimes the system replaces the entire mention with a different word altogether.
How can I turn autoCorrect and autoCapitalize off when the user pushes the @
button?
'
I have even tried rendering an entirely different , but the behavior remains.
ios react-native text keyboard
ios react-native text keyboard
edited Nov 8 at 17:00
asked Nov 8 at 16:54
TIMEX
66.9k267644945
66.9k267644945
This question had a bounty worth +500
reputation from TIMEX that ended 13 hours ago. Grace period ends in 10 hours
This question has not received enough attention.
This question had a bounty worth +500
reputation from TIMEX that ended 13 hours ago. Grace period ends in 10 hours
This question has not received enough attention.
This seems to work for me. snack.expo.io/B1PMIxzTm
– gtfargo
Nov 8 at 17:46
@gtfargo it may work on snack but it doesn't work on real device (exact code)
– TIMEX
Nov 8 at 20:02
which version of react-native are you using?
– lazreg87
Nov 13 at 18:21
Would you please upload a sample project?
– Mojtaba Hosseini
Nov 15 at 10:04
@MojtabaHosseini, Absolutely it is an issue fromiOS
native side and should be fixed from the native side of the project, but I add an innovative solution for solving this problem.
– AmerllicA
Nov 15 at 13:40
|
show 3 more comments
This seems to work for me. snack.expo.io/B1PMIxzTm
– gtfargo
Nov 8 at 17:46
@gtfargo it may work on snack but it doesn't work on real device (exact code)
– TIMEX
Nov 8 at 20:02
which version of react-native are you using?
– lazreg87
Nov 13 at 18:21
Would you please upload a sample project?
– Mojtaba Hosseini
Nov 15 at 10:04
@MojtabaHosseini, Absolutely it is an issue fromiOS
native side and should be fixed from the native side of the project, but I add an innovative solution for solving this problem.
– AmerllicA
Nov 15 at 13:40
This seems to work for me. snack.expo.io/B1PMIxzTm
– gtfargo
Nov 8 at 17:46
This seems to work for me. snack.expo.io/B1PMIxzTm
– gtfargo
Nov 8 at 17:46
@gtfargo it may work on snack but it doesn't work on real device (exact code)
– TIMEX
Nov 8 at 20:02
@gtfargo it may work on snack but it doesn't work on real device (exact code)
– TIMEX
Nov 8 at 20:02
which version of react-native are you using?
– lazreg87
Nov 13 at 18:21
which version of react-native are you using?
– lazreg87
Nov 13 at 18:21
Would you please upload a sample project?
– Mojtaba Hosseini
Nov 15 at 10:04
Would you please upload a sample project?
– Mojtaba Hosseini
Nov 15 at 10:04
@MojtabaHosseini, Absolutely it is an issue from
iOS
native side and should be fixed from the native side of the project, but I add an innovative solution for solving this problem.– AmerllicA
Nov 15 at 13:40
@MojtabaHosseini, Absolutely it is an issue from
iOS
native side and should be fixed from the native side of the project, but I add an innovative solution for solving this problem.– AmerllicA
Nov 15 at 13:40
|
show 3 more comments
3 Answers
3
active
oldest
votes
up vote
9
down vote
TL;DR: you should close and re-launch your keyboard after the TextInput
autoCorrect
toggling value.
Buddy, this is not your fault, I had the same issue on autoFocus
of react native
TextInput
component. I set a state
name for the TextInput
editable
prop and then with the pressing pencil
button I change the editable
props. The designer told me after the TextInput
made editable the cursor should be focused, so I use the isEditable
state for autoFocus
prop, see below:
state = {
isEditable: false
};
handleEdit = () => {
const { isEditable } = this.state;
this.setState({ isEditable: !isEditable });
};
<TextInput
autoFocus={isEditable}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
/>
Then I press the edit button and it turns to:
But it is not focused and the Keyboard didn't launch, I sought and found this link, the TextInput
does not change/update some of its props after componentDidMount
. ☹️. Also, it is not different in iOS
or Android
, both of them has this issue, I used ref
to focus on this TextInput
after the isEditable
state made true
. see following code:
<TextInput
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
componentDidUpdate() {
const { isEditable } = this.state;
if (isEditable) {
this.input.focus();
}
}
And your case:
Absolutely you can not use ref
because the autoCorrect
should render with react native
and it is not like focus()
and blur()
so JavaScript
cannot access to it.
I make a test shape for your case, I create another button like a star for toggling autoCorrect
value alongside my edit button. the filled star means autoCorrect
is true
and the lined star means autoCorrect
is false
, now see the test area code and view:
state = {
isEditable: false,
correct: true
};
handleCorrect = () => {
const { correct } = this.state;
this.setState({ correct: !correct });
};
<TextInput
autoCorrect={correct}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
In the above photo, it is so clear the autoCorrect
rendered as true
, so it is enabled:
When I write a wrong Persian word the auto-correction show its suggestion, now time to press the star button:
Wow, the autoCorrection
is false
in the above situation but still we see the auto-correction of the cellphone. it is just like autoFocus
it is rendered in the first render and after it, the TextInput
could not change/update its props. suddenly I press edit button:
And I press the edit button again, so surely, you realized the autoCorrect
is false
now, ok now see what I saw:
The autoCorrect
remained false
by my double pressing edit button and now the auto-correction of device disappears completely. I don't know it is a bug or my bad understanding but I realized in this test area, for update autoCorrect
value, we should do something after changing its value to close the iPhone keyboard and then re-launch it. the main thing that TextInput
has issued is the launched keyboard.
For my test, when I pressed the edit button the editable
prop of the TextInput
is changed to false and the keyboard is closed, so when I pressed the edit button again, the TextInput
get focused and keyboard re-launched with new autoCorrect
value. This is The Secret.
Solution:
You should do something, to close and open again the iOS keyboard with the new autoCorrect
value. for the test case that I wrote for your question, I decided to do a hybrid innovative solution, using ref
and the callback of setState
:
handleCorrect = () => {
const { correct } = this.state;
this.input.blur(); //-- this line close the keyboard
this.setState({ correct: !correct },
() => {
setTimeout(() => this.input.focus(), 50);
//-- above line re-launch keyboard after 50 milliseconds
//-- this 50 milliseconds is for waiting to closing keyborad finish
}
);
};
<TextInput
autoCorrect={correct}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
And after pressing the star button the keyboard close and re-launch and the auto-correction disappear completely.
Note: obviously, I summarized some other codes like destructuring and writing class or extends and etc for better human readability.
Does the close/reopen keyboard cause animation? Is it possible to disable that animation?
– TIMEX
Nov 15 at 20:11
Dear @TIMEX, I don't know what kind of animation do you mean. but thetrue
autoCorrect
keyboard for the closing, slide down to the bottom of the screen, and it longs about 20 to 50 milliseconds then thefalse
autoCorrect
keyboard re-launch slide up about 20 to 50 milliseconds and it is inevitable. this is a trick and it just comes to my mind. I never have seen this trick before. maybe there is someiOS
native solution, but I don't think so.
– AmerllicA
Nov 15 at 22:28
Dear @TIMEX, I asked twoiOS
developers in my current workplace, both of them told me the animation of keyboard open/close is based on the OS config that the user can config it. they told me it is not possible to disable this animation onnative
side whether on thereact-native
side.
– AmerllicA
yesterday
add a comment |
up vote
0
down vote
The problem isn't in your code completely(except Regex part which didnt work in my device) but how react native renders Keyboard.
I created a sample that along with your initial code also changes backgroundcolor of the screen.
You will find that color changes instantly when '@' is entered whereas the keyboard doesn't.
Unless you reload the keyboard. For this if you un-comment the code it dismisses keyboard once and once you tap back on textInput the new Keyboard without autoCorrect and autoCapitalize is shown.
state = {
searchQuery: null,
isFocused: true,
}
constructor(props) {
super(props);
this.onChangeText = this.onChangeText.bind(this);
}
onChangeText = (val) => {
const suggestTrigger = val.match(/@[A-Za-z0-9]*$/i) //grab "@" trigger
const searchQuery = (suggestTrigger && suggestTrigger.length > 0) ? suggestTrigger[0] : null;
// Un comment this to reload
// if(searchQuery && this.state.isFocused) {
// this.setState({
// isFocused: false
// });
// Keyboard.dismiss();
// // this.myTextInput.focus()
// }
this.setState({
searchQuery: searchQuery
})
}
render() {
const { searchQuery } = this.state
return (
<View style={[styles.container,
{
backgroundColor: searchQuery ? "red": "green"}
]}>
<TextInput
style={{width: 300, height: 50, borderWidth: 1}}
ref={(ref)=>{this.myTextInput = ref}}
autoCapitalize={searchQuery ? "none" : "sentences"}
autoCorrect={searchQuery ? false : true}
onChangeText={this.onChangeText}
/>
</View>
);
}
Since now we know the main cause of error maybe a better solution can be reached.
add a comment |
up vote
0
down vote
I have 2 suggestions:
First, have you tried using the autoCorrect fallback?
spellCheck={this.state.searchQuery ? false : true}
Second, have you tried with native code (Obj-C / Swift)?
import { Platform, TextInput, View } from 'react-native';
import { iOSAutoCorrect } from './your-native-code';
const shouldWork = Platform.OS === 'ios' ? <iOSAutoCorrect /> : <TextInput
autoCapitalize={this.state.searchQuery ? "none" : "sentences"}
autoCorrect={this.state.searchQuery ? false : true}
onChangeText={this.onChangeText} />
return (<View>{shouldWork}</View>);
In iOSAutoCorrect you should use a UITextView. Then set its proper value depending on your condition:
textField.autocorrectionType = UITextAutocorrectionTypeNo; // or UITextAutocorrectionTypeYes
I have free-coded, thus the code is untested and might contain bugs. Good luck.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
TL;DR: you should close and re-launch your keyboard after the TextInput
autoCorrect
toggling value.
Buddy, this is not your fault, I had the same issue on autoFocus
of react native
TextInput
component. I set a state
name for the TextInput
editable
prop and then with the pressing pencil
button I change the editable
props. The designer told me after the TextInput
made editable the cursor should be focused, so I use the isEditable
state for autoFocus
prop, see below:
state = {
isEditable: false
};
handleEdit = () => {
const { isEditable } = this.state;
this.setState({ isEditable: !isEditable });
};
<TextInput
autoFocus={isEditable}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
/>
Then I press the edit button and it turns to:
But it is not focused and the Keyboard didn't launch, I sought and found this link, the TextInput
does not change/update some of its props after componentDidMount
. ☹️. Also, it is not different in iOS
or Android
, both of them has this issue, I used ref
to focus on this TextInput
after the isEditable
state made true
. see following code:
<TextInput
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
componentDidUpdate() {
const { isEditable } = this.state;
if (isEditable) {
this.input.focus();
}
}
And your case:
Absolutely you can not use ref
because the autoCorrect
should render with react native
and it is not like focus()
and blur()
so JavaScript
cannot access to it.
I make a test shape for your case, I create another button like a star for toggling autoCorrect
value alongside my edit button. the filled star means autoCorrect
is true
and the lined star means autoCorrect
is false
, now see the test area code and view:
state = {
isEditable: false,
correct: true
};
handleCorrect = () => {
const { correct } = this.state;
this.setState({ correct: !correct });
};
<TextInput
autoCorrect={correct}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
In the above photo, it is so clear the autoCorrect
rendered as true
, so it is enabled:
When I write a wrong Persian word the auto-correction show its suggestion, now time to press the star button:
Wow, the autoCorrection
is false
in the above situation but still we see the auto-correction of the cellphone. it is just like autoFocus
it is rendered in the first render and after it, the TextInput
could not change/update its props. suddenly I press edit button:
And I press the edit button again, so surely, you realized the autoCorrect
is false
now, ok now see what I saw:
The autoCorrect
remained false
by my double pressing edit button and now the auto-correction of device disappears completely. I don't know it is a bug or my bad understanding but I realized in this test area, for update autoCorrect
value, we should do something after changing its value to close the iPhone keyboard and then re-launch it. the main thing that TextInput
has issued is the launched keyboard.
For my test, when I pressed the edit button the editable
prop of the TextInput
is changed to false and the keyboard is closed, so when I pressed the edit button again, the TextInput
get focused and keyboard re-launched with new autoCorrect
value. This is The Secret.
Solution:
You should do something, to close and open again the iOS keyboard with the new autoCorrect
value. for the test case that I wrote for your question, I decided to do a hybrid innovative solution, using ref
and the callback of setState
:
handleCorrect = () => {
const { correct } = this.state;
this.input.blur(); //-- this line close the keyboard
this.setState({ correct: !correct },
() => {
setTimeout(() => this.input.focus(), 50);
//-- above line re-launch keyboard after 50 milliseconds
//-- this 50 milliseconds is for waiting to closing keyborad finish
}
);
};
<TextInput
autoCorrect={correct}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
And after pressing the star button the keyboard close and re-launch and the auto-correction disappear completely.
Note: obviously, I summarized some other codes like destructuring and writing class or extends and etc for better human readability.
Does the close/reopen keyboard cause animation? Is it possible to disable that animation?
– TIMEX
Nov 15 at 20:11
Dear @TIMEX, I don't know what kind of animation do you mean. but thetrue
autoCorrect
keyboard for the closing, slide down to the bottom of the screen, and it longs about 20 to 50 milliseconds then thefalse
autoCorrect
keyboard re-launch slide up about 20 to 50 milliseconds and it is inevitable. this is a trick and it just comes to my mind. I never have seen this trick before. maybe there is someiOS
native solution, but I don't think so.
– AmerllicA
Nov 15 at 22:28
Dear @TIMEX, I asked twoiOS
developers in my current workplace, both of them told me the animation of keyboard open/close is based on the OS config that the user can config it. they told me it is not possible to disable this animation onnative
side whether on thereact-native
side.
– AmerllicA
yesterday
add a comment |
up vote
9
down vote
TL;DR: you should close and re-launch your keyboard after the TextInput
autoCorrect
toggling value.
Buddy, this is not your fault, I had the same issue on autoFocus
of react native
TextInput
component. I set a state
name for the TextInput
editable
prop and then with the pressing pencil
button I change the editable
props. The designer told me after the TextInput
made editable the cursor should be focused, so I use the isEditable
state for autoFocus
prop, see below:
state = {
isEditable: false
};
handleEdit = () => {
const { isEditable } = this.state;
this.setState({ isEditable: !isEditable });
};
<TextInput
autoFocus={isEditable}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
/>
Then I press the edit button and it turns to:
But it is not focused and the Keyboard didn't launch, I sought and found this link, the TextInput
does not change/update some of its props after componentDidMount
. ☹️. Also, it is not different in iOS
or Android
, both of them has this issue, I used ref
to focus on this TextInput
after the isEditable
state made true
. see following code:
<TextInput
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
componentDidUpdate() {
const { isEditable } = this.state;
if (isEditable) {
this.input.focus();
}
}
And your case:
Absolutely you can not use ref
because the autoCorrect
should render with react native
and it is not like focus()
and blur()
so JavaScript
cannot access to it.
I make a test shape for your case, I create another button like a star for toggling autoCorrect
value alongside my edit button. the filled star means autoCorrect
is true
and the lined star means autoCorrect
is false
, now see the test area code and view:
state = {
isEditable: false,
correct: true
};
handleCorrect = () => {
const { correct } = this.state;
this.setState({ correct: !correct });
};
<TextInput
autoCorrect={correct}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
In the above photo, it is so clear the autoCorrect
rendered as true
, so it is enabled:
When I write a wrong Persian word the auto-correction show its suggestion, now time to press the star button:
Wow, the autoCorrection
is false
in the above situation but still we see the auto-correction of the cellphone. it is just like autoFocus
it is rendered in the first render and after it, the TextInput
could not change/update its props. suddenly I press edit button:
And I press the edit button again, so surely, you realized the autoCorrect
is false
now, ok now see what I saw:
The autoCorrect
remained false
by my double pressing edit button and now the auto-correction of device disappears completely. I don't know it is a bug or my bad understanding but I realized in this test area, for update autoCorrect
value, we should do something after changing its value to close the iPhone keyboard and then re-launch it. the main thing that TextInput
has issued is the launched keyboard.
For my test, when I pressed the edit button the editable
prop of the TextInput
is changed to false and the keyboard is closed, so when I pressed the edit button again, the TextInput
get focused and keyboard re-launched with new autoCorrect
value. This is The Secret.
Solution:
You should do something, to close and open again the iOS keyboard with the new autoCorrect
value. for the test case that I wrote for your question, I decided to do a hybrid innovative solution, using ref
and the callback of setState
:
handleCorrect = () => {
const { correct } = this.state;
this.input.blur(); //-- this line close the keyboard
this.setState({ correct: !correct },
() => {
setTimeout(() => this.input.focus(), 50);
//-- above line re-launch keyboard after 50 milliseconds
//-- this 50 milliseconds is for waiting to closing keyborad finish
}
);
};
<TextInput
autoCorrect={correct}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
And after pressing the star button the keyboard close and re-launch and the auto-correction disappear completely.
Note: obviously, I summarized some other codes like destructuring and writing class or extends and etc for better human readability.
Does the close/reopen keyboard cause animation? Is it possible to disable that animation?
– TIMEX
Nov 15 at 20:11
Dear @TIMEX, I don't know what kind of animation do you mean. but thetrue
autoCorrect
keyboard for the closing, slide down to the bottom of the screen, and it longs about 20 to 50 milliseconds then thefalse
autoCorrect
keyboard re-launch slide up about 20 to 50 milliseconds and it is inevitable. this is a trick and it just comes to my mind. I never have seen this trick before. maybe there is someiOS
native solution, but I don't think so.
– AmerllicA
Nov 15 at 22:28
Dear @TIMEX, I asked twoiOS
developers in my current workplace, both of them told me the animation of keyboard open/close is based on the OS config that the user can config it. they told me it is not possible to disable this animation onnative
side whether on thereact-native
side.
– AmerllicA
yesterday
add a comment |
up vote
9
down vote
up vote
9
down vote
TL;DR: you should close and re-launch your keyboard after the TextInput
autoCorrect
toggling value.
Buddy, this is not your fault, I had the same issue on autoFocus
of react native
TextInput
component. I set a state
name for the TextInput
editable
prop and then with the pressing pencil
button I change the editable
props. The designer told me after the TextInput
made editable the cursor should be focused, so I use the isEditable
state for autoFocus
prop, see below:
state = {
isEditable: false
};
handleEdit = () => {
const { isEditable } = this.state;
this.setState({ isEditable: !isEditable });
};
<TextInput
autoFocus={isEditable}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
/>
Then I press the edit button and it turns to:
But it is not focused and the Keyboard didn't launch, I sought and found this link, the TextInput
does not change/update some of its props after componentDidMount
. ☹️. Also, it is not different in iOS
or Android
, both of them has this issue, I used ref
to focus on this TextInput
after the isEditable
state made true
. see following code:
<TextInput
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
componentDidUpdate() {
const { isEditable } = this.state;
if (isEditable) {
this.input.focus();
}
}
And your case:
Absolutely you can not use ref
because the autoCorrect
should render with react native
and it is not like focus()
and blur()
so JavaScript
cannot access to it.
I make a test shape for your case, I create another button like a star for toggling autoCorrect
value alongside my edit button. the filled star means autoCorrect
is true
and the lined star means autoCorrect
is false
, now see the test area code and view:
state = {
isEditable: false,
correct: true
};
handleCorrect = () => {
const { correct } = this.state;
this.setState({ correct: !correct });
};
<TextInput
autoCorrect={correct}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
In the above photo, it is so clear the autoCorrect
rendered as true
, so it is enabled:
When I write a wrong Persian word the auto-correction show its suggestion, now time to press the star button:
Wow, the autoCorrection
is false
in the above situation but still we see the auto-correction of the cellphone. it is just like autoFocus
it is rendered in the first render and after it, the TextInput
could not change/update its props. suddenly I press edit button:
And I press the edit button again, so surely, you realized the autoCorrect
is false
now, ok now see what I saw:
The autoCorrect
remained false
by my double pressing edit button and now the auto-correction of device disappears completely. I don't know it is a bug or my bad understanding but I realized in this test area, for update autoCorrect
value, we should do something after changing its value to close the iPhone keyboard and then re-launch it. the main thing that TextInput
has issued is the launched keyboard.
For my test, when I pressed the edit button the editable
prop of the TextInput
is changed to false and the keyboard is closed, so when I pressed the edit button again, the TextInput
get focused and keyboard re-launched with new autoCorrect
value. This is The Secret.
Solution:
You should do something, to close and open again the iOS keyboard with the new autoCorrect
value. for the test case that I wrote for your question, I decided to do a hybrid innovative solution, using ref
and the callback of setState
:
handleCorrect = () => {
const { correct } = this.state;
this.input.blur(); //-- this line close the keyboard
this.setState({ correct: !correct },
() => {
setTimeout(() => this.input.focus(), 50);
//-- above line re-launch keyboard after 50 milliseconds
//-- this 50 milliseconds is for waiting to closing keyborad finish
}
);
};
<TextInput
autoCorrect={correct}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
And after pressing the star button the keyboard close and re-launch and the auto-correction disappear completely.
Note: obviously, I summarized some other codes like destructuring and writing class or extends and etc for better human readability.
TL;DR: you should close and re-launch your keyboard after the TextInput
autoCorrect
toggling value.
Buddy, this is not your fault, I had the same issue on autoFocus
of react native
TextInput
component. I set a state
name for the TextInput
editable
prop and then with the pressing pencil
button I change the editable
props. The designer told me after the TextInput
made editable the cursor should be focused, so I use the isEditable
state for autoFocus
prop, see below:
state = {
isEditable: false
};
handleEdit = () => {
const { isEditable } = this.state;
this.setState({ isEditable: !isEditable });
};
<TextInput
autoFocus={isEditable}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
/>
Then I press the edit button and it turns to:
But it is not focused and the Keyboard didn't launch, I sought and found this link, the TextInput
does not change/update some of its props after componentDidMount
. ☹️. Also, it is not different in iOS
or Android
, both of them has this issue, I used ref
to focus on this TextInput
after the isEditable
state made true
. see following code:
<TextInput
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
componentDidUpdate() {
const { isEditable } = this.state;
if (isEditable) {
this.input.focus();
}
}
And your case:
Absolutely you can not use ref
because the autoCorrect
should render with react native
and it is not like focus()
and blur()
so JavaScript
cannot access to it.
I make a test shape for your case, I create another button like a star for toggling autoCorrect
value alongside my edit button. the filled star means autoCorrect
is true
and the lined star means autoCorrect
is false
, now see the test area code and view:
state = {
isEditable: false,
correct: true
};
handleCorrect = () => {
const { correct } = this.state;
this.setState({ correct: !correct });
};
<TextInput
autoCorrect={correct}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
In the above photo, it is so clear the autoCorrect
rendered as true
, so it is enabled:
When I write a wrong Persian word the auto-correction show its suggestion, now time to press the star button:
Wow, the autoCorrection
is false
in the above situation but still we see the auto-correction of the cellphone. it is just like autoFocus
it is rendered in the first render and after it, the TextInput
could not change/update its props. suddenly I press edit button:
And I press the edit button again, so surely, you realized the autoCorrect
is false
now, ok now see what I saw:
The autoCorrect
remained false
by my double pressing edit button and now the auto-correction of device disappears completely. I don't know it is a bug or my bad understanding but I realized in this test area, for update autoCorrect
value, we should do something after changing its value to close the iPhone keyboard and then re-launch it. the main thing that TextInput
has issued is the launched keyboard.
For my test, when I pressed the edit button the editable
prop of the TextInput
is changed to false and the keyboard is closed, so when I pressed the edit button again, the TextInput
get focused and keyboard re-launched with new autoCorrect
value. This is The Secret.
Solution:
You should do something, to close and open again the iOS keyboard with the new autoCorrect
value. for the test case that I wrote for your question, I decided to do a hybrid innovative solution, using ref
and the callback of setState
:
handleCorrect = () => {
const { correct } = this.state;
this.input.blur(); //-- this line close the keyboard
this.setState({ correct: !correct },
() => {
setTimeout(() => this.input.focus(), 50);
//-- above line re-launch keyboard after 50 milliseconds
//-- this 50 milliseconds is for waiting to closing keyborad finish
}
);
};
<TextInput
autoCorrect={correct}
editable={isEditable}
style={styles.textNameEditable}
defaultValue={text}
numberOfLines={1}
ref={input => {
this.input = input;
}}
/>
And after pressing the star button the keyboard close and re-launch and the auto-correction disappear completely.
Note: obviously, I summarized some other codes like destructuring and writing class or extends and etc for better human readability.
edited Nov 15 at 13:44
answered Nov 15 at 13:38
AmerllicA
2,64311033
2,64311033
Does the close/reopen keyboard cause animation? Is it possible to disable that animation?
– TIMEX
Nov 15 at 20:11
Dear @TIMEX, I don't know what kind of animation do you mean. but thetrue
autoCorrect
keyboard for the closing, slide down to the bottom of the screen, and it longs about 20 to 50 milliseconds then thefalse
autoCorrect
keyboard re-launch slide up about 20 to 50 milliseconds and it is inevitable. this is a trick and it just comes to my mind. I never have seen this trick before. maybe there is someiOS
native solution, but I don't think so.
– AmerllicA
Nov 15 at 22:28
Dear @TIMEX, I asked twoiOS
developers in my current workplace, both of them told me the animation of keyboard open/close is based on the OS config that the user can config it. they told me it is not possible to disable this animation onnative
side whether on thereact-native
side.
– AmerllicA
yesterday
add a comment |
Does the close/reopen keyboard cause animation? Is it possible to disable that animation?
– TIMEX
Nov 15 at 20:11
Dear @TIMEX, I don't know what kind of animation do you mean. but thetrue
autoCorrect
keyboard for the closing, slide down to the bottom of the screen, and it longs about 20 to 50 milliseconds then thefalse
autoCorrect
keyboard re-launch slide up about 20 to 50 milliseconds and it is inevitable. this is a trick and it just comes to my mind. I never have seen this trick before. maybe there is someiOS
native solution, but I don't think so.
– AmerllicA
Nov 15 at 22:28
Dear @TIMEX, I asked twoiOS
developers in my current workplace, both of them told me the animation of keyboard open/close is based on the OS config that the user can config it. they told me it is not possible to disable this animation onnative
side whether on thereact-native
side.
– AmerllicA
yesterday
Does the close/reopen keyboard cause animation? Is it possible to disable that animation?
– TIMEX
Nov 15 at 20:11
Does the close/reopen keyboard cause animation? Is it possible to disable that animation?
– TIMEX
Nov 15 at 20:11
Dear @TIMEX, I don't know what kind of animation do you mean. but the
true
autoCorrect
keyboard for the closing, slide down to the bottom of the screen, and it longs about 20 to 50 milliseconds then the false
autoCorrect
keyboard re-launch slide up about 20 to 50 milliseconds and it is inevitable. this is a trick and it just comes to my mind. I never have seen this trick before. maybe there is some iOS
native solution, but I don't think so.– AmerllicA
Nov 15 at 22:28
Dear @TIMEX, I don't know what kind of animation do you mean. but the
true
autoCorrect
keyboard for the closing, slide down to the bottom of the screen, and it longs about 20 to 50 milliseconds then the false
autoCorrect
keyboard re-launch slide up about 20 to 50 milliseconds and it is inevitable. this is a trick and it just comes to my mind. I never have seen this trick before. maybe there is some iOS
native solution, but I don't think so.– AmerllicA
Nov 15 at 22:28
Dear @TIMEX, I asked two
iOS
developers in my current workplace, both of them told me the animation of keyboard open/close is based on the OS config that the user can config it. they told me it is not possible to disable this animation on native
side whether on the react-native
side.– AmerllicA
yesterday
Dear @TIMEX, I asked two
iOS
developers in my current workplace, both of them told me the animation of keyboard open/close is based on the OS config that the user can config it. they told me it is not possible to disable this animation on native
side whether on the react-native
side.– AmerllicA
yesterday
add a comment |
up vote
0
down vote
The problem isn't in your code completely(except Regex part which didnt work in my device) but how react native renders Keyboard.
I created a sample that along with your initial code also changes backgroundcolor of the screen.
You will find that color changes instantly when '@' is entered whereas the keyboard doesn't.
Unless you reload the keyboard. For this if you un-comment the code it dismisses keyboard once and once you tap back on textInput the new Keyboard without autoCorrect and autoCapitalize is shown.
state = {
searchQuery: null,
isFocused: true,
}
constructor(props) {
super(props);
this.onChangeText = this.onChangeText.bind(this);
}
onChangeText = (val) => {
const suggestTrigger = val.match(/@[A-Za-z0-9]*$/i) //grab "@" trigger
const searchQuery = (suggestTrigger && suggestTrigger.length > 0) ? suggestTrigger[0] : null;
// Un comment this to reload
// if(searchQuery && this.state.isFocused) {
// this.setState({
// isFocused: false
// });
// Keyboard.dismiss();
// // this.myTextInput.focus()
// }
this.setState({
searchQuery: searchQuery
})
}
render() {
const { searchQuery } = this.state
return (
<View style={[styles.container,
{
backgroundColor: searchQuery ? "red": "green"}
]}>
<TextInput
style={{width: 300, height: 50, borderWidth: 1}}
ref={(ref)=>{this.myTextInput = ref}}
autoCapitalize={searchQuery ? "none" : "sentences"}
autoCorrect={searchQuery ? false : true}
onChangeText={this.onChangeText}
/>
</View>
);
}
Since now we know the main cause of error maybe a better solution can be reached.
add a comment |
up vote
0
down vote
The problem isn't in your code completely(except Regex part which didnt work in my device) but how react native renders Keyboard.
I created a sample that along with your initial code also changes backgroundcolor of the screen.
You will find that color changes instantly when '@' is entered whereas the keyboard doesn't.
Unless you reload the keyboard. For this if you un-comment the code it dismisses keyboard once and once you tap back on textInput the new Keyboard without autoCorrect and autoCapitalize is shown.
state = {
searchQuery: null,
isFocused: true,
}
constructor(props) {
super(props);
this.onChangeText = this.onChangeText.bind(this);
}
onChangeText = (val) => {
const suggestTrigger = val.match(/@[A-Za-z0-9]*$/i) //grab "@" trigger
const searchQuery = (suggestTrigger && suggestTrigger.length > 0) ? suggestTrigger[0] : null;
// Un comment this to reload
// if(searchQuery && this.state.isFocused) {
// this.setState({
// isFocused: false
// });
// Keyboard.dismiss();
// // this.myTextInput.focus()
// }
this.setState({
searchQuery: searchQuery
})
}
render() {
const { searchQuery } = this.state
return (
<View style={[styles.container,
{
backgroundColor: searchQuery ? "red": "green"}
]}>
<TextInput
style={{width: 300, height: 50, borderWidth: 1}}
ref={(ref)=>{this.myTextInput = ref}}
autoCapitalize={searchQuery ? "none" : "sentences"}
autoCorrect={searchQuery ? false : true}
onChangeText={this.onChangeText}
/>
</View>
);
}
Since now we know the main cause of error maybe a better solution can be reached.
add a comment |
up vote
0
down vote
up vote
0
down vote
The problem isn't in your code completely(except Regex part which didnt work in my device) but how react native renders Keyboard.
I created a sample that along with your initial code also changes backgroundcolor of the screen.
You will find that color changes instantly when '@' is entered whereas the keyboard doesn't.
Unless you reload the keyboard. For this if you un-comment the code it dismisses keyboard once and once you tap back on textInput the new Keyboard without autoCorrect and autoCapitalize is shown.
state = {
searchQuery: null,
isFocused: true,
}
constructor(props) {
super(props);
this.onChangeText = this.onChangeText.bind(this);
}
onChangeText = (val) => {
const suggestTrigger = val.match(/@[A-Za-z0-9]*$/i) //grab "@" trigger
const searchQuery = (suggestTrigger && suggestTrigger.length > 0) ? suggestTrigger[0] : null;
// Un comment this to reload
// if(searchQuery && this.state.isFocused) {
// this.setState({
// isFocused: false
// });
// Keyboard.dismiss();
// // this.myTextInput.focus()
// }
this.setState({
searchQuery: searchQuery
})
}
render() {
const { searchQuery } = this.state
return (
<View style={[styles.container,
{
backgroundColor: searchQuery ? "red": "green"}
]}>
<TextInput
style={{width: 300, height: 50, borderWidth: 1}}
ref={(ref)=>{this.myTextInput = ref}}
autoCapitalize={searchQuery ? "none" : "sentences"}
autoCorrect={searchQuery ? false : true}
onChangeText={this.onChangeText}
/>
</View>
);
}
Since now we know the main cause of error maybe a better solution can be reached.
The problem isn't in your code completely(except Regex part which didnt work in my device) but how react native renders Keyboard.
I created a sample that along with your initial code also changes backgroundcolor of the screen.
You will find that color changes instantly when '@' is entered whereas the keyboard doesn't.
Unless you reload the keyboard. For this if you un-comment the code it dismisses keyboard once and once you tap back on textInput the new Keyboard without autoCorrect and autoCapitalize is shown.
state = {
searchQuery: null,
isFocused: true,
}
constructor(props) {
super(props);
this.onChangeText = this.onChangeText.bind(this);
}
onChangeText = (val) => {
const suggestTrigger = val.match(/@[A-Za-z0-9]*$/i) //grab "@" trigger
const searchQuery = (suggestTrigger && suggestTrigger.length > 0) ? suggestTrigger[0] : null;
// Un comment this to reload
// if(searchQuery && this.state.isFocused) {
// this.setState({
// isFocused: false
// });
// Keyboard.dismiss();
// // this.myTextInput.focus()
// }
this.setState({
searchQuery: searchQuery
})
}
render() {
const { searchQuery } = this.state
return (
<View style={[styles.container,
{
backgroundColor: searchQuery ? "red": "green"}
]}>
<TextInput
style={{width: 300, height: 50, borderWidth: 1}}
ref={(ref)=>{this.myTextInput = ref}}
autoCapitalize={searchQuery ? "none" : "sentences"}
autoCorrect={searchQuery ? false : true}
onChangeText={this.onChangeText}
/>
</View>
);
}
Since now we know the main cause of error maybe a better solution can be reached.
answered Nov 14 at 15:35
Jerin
2,88331333
2,88331333
add a comment |
add a comment |
up vote
0
down vote
I have 2 suggestions:
First, have you tried using the autoCorrect fallback?
spellCheck={this.state.searchQuery ? false : true}
Second, have you tried with native code (Obj-C / Swift)?
import { Platform, TextInput, View } from 'react-native';
import { iOSAutoCorrect } from './your-native-code';
const shouldWork = Platform.OS === 'ios' ? <iOSAutoCorrect /> : <TextInput
autoCapitalize={this.state.searchQuery ? "none" : "sentences"}
autoCorrect={this.state.searchQuery ? false : true}
onChangeText={this.onChangeText} />
return (<View>{shouldWork}</View>);
In iOSAutoCorrect you should use a UITextView. Then set its proper value depending on your condition:
textField.autocorrectionType = UITextAutocorrectionTypeNo; // or UITextAutocorrectionTypeYes
I have free-coded, thus the code is untested and might contain bugs. Good luck.
add a comment |
up vote
0
down vote
I have 2 suggestions:
First, have you tried using the autoCorrect fallback?
spellCheck={this.state.searchQuery ? false : true}
Second, have you tried with native code (Obj-C / Swift)?
import { Platform, TextInput, View } from 'react-native';
import { iOSAutoCorrect } from './your-native-code';
const shouldWork = Platform.OS === 'ios' ? <iOSAutoCorrect /> : <TextInput
autoCapitalize={this.state.searchQuery ? "none" : "sentences"}
autoCorrect={this.state.searchQuery ? false : true}
onChangeText={this.onChangeText} />
return (<View>{shouldWork}</View>);
In iOSAutoCorrect you should use a UITextView. Then set its proper value depending on your condition:
textField.autocorrectionType = UITextAutocorrectionTypeNo; // or UITextAutocorrectionTypeYes
I have free-coded, thus the code is untested and might contain bugs. Good luck.
add a comment |
up vote
0
down vote
up vote
0
down vote
I have 2 suggestions:
First, have you tried using the autoCorrect fallback?
spellCheck={this.state.searchQuery ? false : true}
Second, have you tried with native code (Obj-C / Swift)?
import { Platform, TextInput, View } from 'react-native';
import { iOSAutoCorrect } from './your-native-code';
const shouldWork = Platform.OS === 'ios' ? <iOSAutoCorrect /> : <TextInput
autoCapitalize={this.state.searchQuery ? "none" : "sentences"}
autoCorrect={this.state.searchQuery ? false : true}
onChangeText={this.onChangeText} />
return (<View>{shouldWork}</View>);
In iOSAutoCorrect you should use a UITextView. Then set its proper value depending on your condition:
textField.autocorrectionType = UITextAutocorrectionTypeNo; // or UITextAutocorrectionTypeYes
I have free-coded, thus the code is untested and might contain bugs. Good luck.
I have 2 suggestions:
First, have you tried using the autoCorrect fallback?
spellCheck={this.state.searchQuery ? false : true}
Second, have you tried with native code (Obj-C / Swift)?
import { Platform, TextInput, View } from 'react-native';
import { iOSAutoCorrect } from './your-native-code';
const shouldWork = Platform.OS === 'ios' ? <iOSAutoCorrect /> : <TextInput
autoCapitalize={this.state.searchQuery ? "none" : "sentences"}
autoCorrect={this.state.searchQuery ? false : true}
onChangeText={this.onChangeText} />
return (<View>{shouldWork}</View>);
In iOSAutoCorrect you should use a UITextView. Then set its proper value depending on your condition:
textField.autocorrectionType = UITextAutocorrectionTypeNo; // or UITextAutocorrectionTypeYes
I have free-coded, thus the code is untested and might contain bugs. Good luck.
answered yesterday
Gillsoft AB
3,31011231
3,31011231
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212534%2fcannot-turn-autocorrect-to-false-in-react-native-textinput%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This seems to work for me. snack.expo.io/B1PMIxzTm
– gtfargo
Nov 8 at 17:46
@gtfargo it may work on snack but it doesn't work on real device (exact code)
– TIMEX
Nov 8 at 20:02
which version of react-native are you using?
– lazreg87
Nov 13 at 18:21
Would you please upload a sample project?
– Mojtaba Hosseini
Nov 15 at 10:04
@MojtabaHosseini, Absolutely it is an issue from
iOS
native side and should be fixed from the native side of the project, but I add an innovative solution for solving this problem.– AmerllicA
Nov 15 at 13:40