首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用AsyncStorage将用户输入的数据保存到表单中并保存到本地设备中

如何使用AsyncStorage将用户输入的数据保存到表单中并保存到本地设备中
EN

Stack Overflow用户
提问于 2018-10-10 06:06:37
回答 1查看 19关注 0票数 0

现在,我正在尝试将用户输入的一些数据保存到一些表单中,并将这些数据保存在用户的本地设备中。但是,我有一个错误。(下图)

我不知道为什么和如何解决这个问题。下面是我的代码。这是“状态”。

代码语言:javascript
复制
class PersonalInfo1 extends React.Component {
 constructor(props) {
 super(props);
  this.state = {
   userInput: [
     { fullname: '' },
     { middleName: '' },
     { reason: '' },
     { birthPlaceCity: '' },
     { birthPlaceCountry: '' },
     { Citizinchip: '' },
     { aboutMaridge: '' },
     { fromTermOfMaridge: '' },
     { ToTermOfMaridge: '' },
     { nameOfSpouse:'' },
     { birthdateOfSpouse: '' },
     { fromTermOfExMaridge: '' },
     { ToTermOfExMaridge: '' },
     { nameOfExSpouse: '' },
     { birthdateOfExSpouse: '' },
    ],
   };
 }

这是代码的其余部分。

代码语言:javascript
复制
 componentDidMount() {
  AsyncStorage.getItem('userInput')
   .then((userInput) => this.setState({ userInput }));
}

 onChangeText(text, name) {
   AsyncStorage.getItem('userInput')
    .then((userInput) => {
    if (userInput !== null) {
      this.state.userInput[name] = text;
      AsyncStorage.setItem('userInput', userInput);
    }
  });
}


  const { userInput } = this.state;
  return (
    <ScrollView style={styles.container}>
    <InfoHeader navigation={this.props.navigation}>
     申請者情報1 
    </InfoHeader>
    <Notes />
    <QuestionTextSet
      onChangeText={(text) => this.onChangeText(text, 'fullname')}
      placeholder={'例:留学太郎'}
      value={userInput.fullname}
      editable
    >
      姓名(漢字表記)
    </QuestionTextSet>
EN

回答 1

Stack Overflow用户

发布于 2018-10-10 06:34:27

this.state.userInput是索引数组(0,1,2...)使用对象(例如{ fullname:'‘})作为值。因此,您不能通过以下方式访问它:

代码语言:javascript
复制
this.state.userInput[name] = text;

尝试将this.state更改为以下内容:

代码语言:javascript
复制
this.state = {
    userInput:
        {
            fullname: '',
            middleName: '',
            reason: '',
            birthPlaceCity: '',
            birthPlaceCountry: '',
            Citizinchip: '',
            aboutMaridge: '',
            fromTermOfMaridge: '',
            ToTermOfMaridge: '',
            nameOfSpouse: '',
            birthdateOfSpouse: '',
            fromTermOfExMaridge: '',
            ToTermOfExMaridge: '',
            nameOfExSpouse: '',
            birthdateOfExSpouse: ''
        }
};

..。如果这对你来说是可以接受的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52730000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档