官术网_书友最值得收藏!

Uncontrolled input

Uncontrolled input in React Native is not really what it is in web development. In fact, TextInput cannot be uncontrolled entirely. You need to listen to a value change in some way:

  • onChangeText fires every time the text input changes
  • onSubmitEditing fires when the text input's submit button is pressed

Additionally, TextInput by itself is a controlled component. Check further for an explanation. A long time ago, it used to have a prop called controlled that allowed you to specify a Boolean value, but this has changed. The documentation at that time specified the following:

"If you really want this to behave as a controlled component, you can set this to true, but you will probably see flickering, dropped keystrokes, and/or laggy typing, depending on how you process onChange events."
– https://facebook.github.io/react-native/docs/0.7/textinput.html.

I realize that the React Native team did put a lot of effort into addressing these issues and they fixed TextInput. However, TextInput became a controlled input to some extent. For instance, selection on TextInput is managed by React Native within the componentDidUpdate function.

"Selection is also a controlled prop. If the native value doesn't match JS, update to the JS value."

– React Native source code for TextInput:  https://github.com/facebook/react-native/blob/c595509048cc5f6cab360cd2ccbe7c86405baf92/Libraries/Components/TextInput/TextInput.js.

Unless you specify the onChangeText or value props, then your component does not appear to get any more overhead.

The fact is that you can still use refs. Check out the following example to learn how to use React's latest API:

// Chapter 2 / Example 10 / App.js

export default class
App extends React.Component {
constructor(props) {
super(props);

this.inputRef = React.createRef();
}

render = () => (
<TextInput style={{height:50}} ref={ref => this.inputRef = ref} />
);

componentDidMount() {
this.inputRef.focus();
}
}

However, there are some limitations. You cannot ask ref for the input value. Sadly, I find this unlikely to change. If you look at this from the other side, it feels more natural. You probably only need controlled components. The benefit from uncontrolled ones is performance that, as of now, does not differ much. Hence, I doubt that you need uncontrolled components in React Native. I couldn't even come up with a use case where you would need a lot of uncontrolled components because of performance issues.

The closest I could get to leaving a component on its own was by using onSubmitEditing or onEndEditing. Such callbacks can be used like the onChangeText prop. They do not fire until the user presses the Submit/Return button on the native keyboard. Unfortunately, you can probably imagine the case when the user, instead of pressing the expected button, presses the login button instead. In such a case, the state would not be updated with the latest data, because the native keyboard remains opened. Such nuances may lead to incorrect data submission and critical bugs. Be careful.

If you are developing websites using React, don't get discouraged by this section. refs are powerful for brown field websites and are useful for those who cannot afford to rewrite existing pieces into React. If this is your case, please also check out the portals API from React v16  https://reactjs.org/docs/portals.html.
主站蜘蛛池模板: 威海市| 德令哈市| 泽库县| 洛南县| 隆安县| 东平县| 阿图什市| 尉氏县| 水富县| 闽清县| 化隆| 南木林县| 泗阳县| 修文县| 衡南县| 醴陵市| 福安市| 渝北区| 湖州市| 慈溪市| 武邑县| 浠水县| 长沙市| 老河口市| 综艺| 临潭县| 丰城市| 大悟县| 新疆| 青铜峡市| 安丘市| 清远市| 双柏县| 临武县| 乃东县| 哈尔滨市| 大渡口区| 鄂托克前旗| 瓦房店市| 西丰县| 阿克陶县|