Controlled vs Uncontrolled Components
1. Controlled Components:
In a controlled component, the form element's value is controlled by React state. This means that the input's value is updated through the state, and any changes to the input are reflected via the onChangeText or similar event handlers.
Characteristics:
Example of Controlled Component:
import React, { useState } from 'react';
import { TextInput, View, Text } from 'react-native';
const ControlledInput = () => {
const [text, setText] = useState(''); // Controlled state
return (
<View>
<TextInput
value={text} // Controlled by state
onChangeText={newText => setText(newText)} // Updates state
placeholder="Enter text"
style={{ borderColor: 'gray', borderWidth: 1, padding: 16 }}
/>
<Text>Entered Text: {text}</Text>
</View>
);
};
export default ControlledInput;
In this example, the TextInput's value is controlled by the text state, which is updated on every keystroke.
2. Uncontrolled Components:
In an uncontrolled component, the form element maintains its own internal state. You access the value via a reference (ref) when needed, rather than controlling the value via state.
Characteristics:
Example of Uncontrolled Component:
import React, { useRef } from 'react';
import { TextInput, View, Button } from 'react-native';
const UncontrolledInput = () => {
const inputRef = useRef(null); // Ref for the input
const handleSubmit = () => {
const inputValue = inputRef.current.value; // Accessing value via ref
console.log(inputValue);
};
return (
<View>
<TextInput
ref={inputRef} // Uncontrolled via ref
placeholder="Enter text"
style={{ borderColor: 'gray', borderWidth: 1, padding: 16 }}
/>
<Button title="Submit" onPress={handleSubmit} />
</View>
);
};
export default UncontrolledInput;
In this example, the TextInput's value is not controlled by state but can be accessed using a ref when needed (e.g., when the submit button is pressed).
Key Differences:
Choosing between controlled and uncontrolled components in React Native depends on your use case and how much control you need over the form elements. Here’s a guideline on when to use each:
1. Controlled Components:
When to Use:
Why Use Controlled Components:
领英推荐
Example Scenario:
If you have a form where the user’s input should be validated live (e.g., password strength checker or matching confirmation passwords), controlled components are ideal.
2. Uncontrolled Components:
When to Use:
Why Use Uncontrolled Components:
Example Scenario:
If you're building a basic contact form where you just need to capture the user's name, email, and message, and you only care about the data when the user submits the form, using uncontrolled components might make sense.
In general, controlled components are more flexible and powerful for complex use cases, while uncontrolled components are simpler for basic or isolated form fields.
In terms of performance optimisation, uncontrolled components are generally more optimised than controlled components, especially in larger forms or when dealing with complex UI re-renders. Here’s why:
1. Uncontrolled Components (More Optimised in Certain Cases):
2. Controlled Components (Less Optimised, but Flexible):
When Optimisation is Critical:
Optimisation Strategies:
Final Verdict:
In most cases, optimisation can be managed based on the specific needs of your app. For basic forms, uncontrolled components are often the better choice for performance; for dynamic or validated forms, controlled components give you the necessary flexibility despite the extra rendering overhead.