useBinding
The useBinding
hook creates a two-way-binding value that can be passed into views that play a role in updating that value.
function useBinding<T>(initialValue: T): Binding<T>;
Example​
- swiftui-react-native
- SwiftUI
- React Native
const text = useBinding('');
<TextField text={text} />
<Text>Current Value: {text.value}</Text>
@State var text = ""
TextField("Name", text: $text)
Text("Current Value: \(text)")
const [text, setText] = useState('');
<TextInput value={text} onChangeText={(newText) => setText(newText)} />\
<Text>Current Value: {text}</Text>
Manually setting the value​
Sometimes you might want to set the value of a binding manually. You can do this by using the setValue
method on the binding, or, if your binding is a boolean, you can use the toggle
method.
text.setValue('Hi');
showAlert.toggle();