【发布时间】:2022-01-12 13:30:53
【问题描述】:
我的问题是我试图通过 API 调用处理我的输入值,用户定义了他想要的输入。
这是我获取值的地方:
const handleClick = buttonTitle => async () => {
await renderField(buttonTitle).then(response => {
navigation.navigate('FormScreen', {
collectionKey: buttonTitle.slice(7),
paramKey: JSON.stringify(response),
});
});
};
渲染字段是一个API调用,它返回我{"message": [{"_id": "618e4c23db08f70b719f3655", "author": "adicionarei posteriormente", "ceatedAt": "2021-11-12 08:12:32", "field": "abc", "fieldtype": "Text"}, {"_id": "618e4c9ddb08f70b719fae37", "author": "adicionarei posteriormente", "ceatedAt": "2021-11-12 08:14:35", "field": "Animal", "fieldtype": "Text"}]}
然后我有我的表单组件,我在其中获得了一些需要的组件并显示给用户:
const FormScreen = ({route, navigation}) => {
return (
<Container>
<InputBody route={route.params.paramKey} navigation={navigation} />
</Container>
// => handle submit Input it in here ?
);
};
对于我的inputbody 组件,我有以下代码(请记住(body.map 是 api 调用响应):
return (
<>
{Object.keys(Body).length > 0 ? (
Body.map(item => (
<React.Fragment key={uuid.v4()}><Texto>{item.field}</Texto>
{renderContent(item.fieldtype,item.field)}
</React.Fragment>
))
) : (
<ActivityIndicator size="large" color="#eb6b09" />
)}
</>
)
}
然后我有我的渲染内容(我得到字段的类型为string 和字段的名称为string)。
function renderContent(type,field) {
switch(type) {
case 'Numeric':
return <NumberInput key={field} keyboardType="numeric" />
case 'Text':
return <TextInput key={field} />
}
}
记住:每个字段类型可以出现多次。
(例如:我可以有一个表单有超过 1 个文本输入),那么我的问题是:我如何处理我的输入值,知道它可以有任何类型的输入(Numeric or Text)?
obs:我可以显示任何类型的信息。
【问题讨论】:
标签: javascript react-native function jsx