简体   繁体   English

如何在本机反应中显示来自渲染动态输入字段的值

[英]How to Display value from render dynamic Input fields in react native

I wan to get values when user click on button But my form fields is json that I dynamically created example is here https://snack.expo.dev/@john_marsh/form-data我想在用户单击按钮时获取值但是我的表单字段是 json 我动态创建的示例在这里https://snack.expo.dev/@john_mar

Textfield文本域

function Text_field(props) {
 return(
   <View key={props.block.keys}>
     <TextInput  
       style={styles.input}
       placeholderTextColor={'black'}
       defaultValue=""
       placeholder={props.block.label}
       type={props.block.type}/>
     {props.children}
   </View>
  );
}

you need to store your value, see controlled input您需要存储您的价值,请参阅受控输入

Try this:尝试这个:

const [respuestas, setRespuestas] = useState([]);
<TextInput
style={styles.input}
placeholderTextColor={'black'}
defaultValue=''
placeholder={props.block.label}
type={props.block.type}
onChangeText={(value) => {
  setRespuestas({ ...respuestas, [props.block.id]: value });
}}
/>

I declare a variable where I'm going to save all the values identified by an id.我声明了一个变量,我将在其中保存由 id 标识的所有值。 You will get an Object with the values.您将获得带有这些值的 Object。 You can try saving values and print the object with console.log您可以尝试保存值并使用 console.log 打印 object

Formik福米克

Hey, I would really suggest you use Formik to handle Forms in React Native, it already gives you a lot of tools and helps you take all values from all inputs in a form.嘿,我真的建议你在 React Native 中使用 Formik 来处理 Forms,它已经为你提供了很多工具并帮助你从表单中的所有输入中获取所有值。 Give it a try, refactoring is easy too!试一试,重构也很容易!

Here are the docs: https://formik.org/docs/guides/react-native以下是文档: https://formik.org/docs/guides/react-native

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM