React Native使用Redux-Form时keyboardType属性失效,是否有替代方案?
Hey there! I see exactly what's going on here. You've set the keyboardType prop on your Redux-Form Field component, but your custom textAccount component isn't receiving and passing that prop down to the underlying Input from react-native-elements. That's why the keyboard type isn't updating as expected.
Here's how to fix it in two simple steps:
Update your
textAccountcomponent to accept extra props
Use the rest operator (...rest) to capture any additional props you pass to theFieldcomponent (likekeyboardType), alongside theinputandfieldNameyou're already using.Pass those extra props to the
Inputcomponent
Spread therestprops onto theInputso that React Native gets the keyboard type configuration.
Modified textAccount Component
import React from 'react'; import { View } from 'react-native'; import { Input } from 'react-native-elements'; function textAccount(props) { // Capture input, fieldName, and all other props with ...rest const { input, fieldName, ...rest } = props; return ( <View> <Input // Bind Redux-Form's input handlers and value onChangeText={input.onChange} value={input.value} placeholder={fieldName} // Spread all extra props (including keyboardType) onto the Input {...rest} /> </View> ); } export default textAccount;
Your Field Usage (No Changes Needed!)
Your original Field component code is fine—once the textAccount component is updated, it will pick up the keyboardType prop automatically:
<Field name="email" component={textAccount} fieldName="Email" keyboardType="email-address" />
Bonus: This approach works for other Input props too! You can now pass things like secureTextEntry for password fields, autoCapitalize, or maxLength directly through the Field component, and they'll be applied to the underlying input.
内容的提问来源于stack exchange,提问作者Flora

