react-native - 尝试在本机反应中动态创建文本输入
问题描述
我正在创建一个 Todo 应用程序来练习我所学的内容。当用户按下按钮时,我尝试动态创建文本输入。但是,我已经被困了一段时间,无法弄清楚。这是我的代码。任何帮助深表感谢。我的主要问题是用户输入的句柄与文本输入的创建以及如何处理它。
import React from 'react';
import {StyleSheet, View, Button, TextInput, ScrollView} from 'react-
native';
import {Constants} from 'expo';
import FormNewTodo from '../components/FormNewTodo'
const styles = StyleSheet.create ({
appContainer: {
flex: 1,
paddingTop: Constants.statusBarHeight,
backgroundColor: 'white',
},
form: {
padding: 10,
borderColor: 'black',
borderWidth: 1,
},
})
let id = 0
export default class AddTodoForm extends React.Component {
state = {
titleText: '',
formItems: [],
}
handleAddTodo = () => {
this.setState({
formItems: [...this.state.formItems, {id: id++, text: ''},],
})
};
handleformItemsChange = text => {
// pass the formItems state to a variable
let newArray = [...this.state.formItems];
let item = [];
/*Check if it´s the beginning of a new input or to check if the
user is going to an old input to edit it*/
if (text.length === 1)
{
item = newArray.filter(item => item.text === '')}
else {
item = newArray.filter(item => item.text === text[text.length - 1])}
//pass the item to the right element in the array and the update
state
let index = item[0].id;
item[0].text = text;
newArray[index] = item;
this.setState({ formsItems: newArray });
}
handleFormTitleChange = titleText => {
this.setState({titleText: titleText})
}
handleSave = () => {
this.props.onSubmit(this.state)
let id = 0
};
static navigationOptions = ({ navigation }) => {
return {
headerTitle: 'TO DO´s',
headerRight: (
<Button
title="Add"
onPress={() => navigation.navigate('AddTodoForm')}
/>
),
};
};
render() {
return (
<View>
<View>
<TextInput style={styles.form}
value={this.state.titleText}
onChangeText={this.handleFormTitleChange}
placeholder={'Title'}/>
</View>
<ScrollView>
{this.state.formItems.map(item => <FormNewTodo
key={item.id}
onChangeText={this.handleformItemsChange}
value={item.text}
/>)}
</ScrollView>
<Button onPress={this.handleSave} title='Save'/>
<Button onPress={this.handleAddTodo} title='Add Todo'/>
</View>
)
}
}
解决方案
推荐阅读
- java - Android Studio中Java的Clojure单元测试?
- jquery - jQuery - 使用 :after 从 CSS 获取图像
- reactjs - 如何在组件之间传递状态变量
- python - 在 python 中将 int 转换为 str 会产生 TypeError
- forms - Flutter 如何在显示对话框中使用文本表单获取用户输入?
- python - 将新行添加到 Google 表格时触发 Python
- javascript - 单击多次执行的模态按钮代码时(JQuery-JavaScript)
- keras - CNN 识别能否回答“不是这些类中的任何一个”
- sql - 我可以准确查询结果吗
- java - 对整数输入进行排序时如何在输出中显示“主题”