【发布时间】:2017-07-21 18:10:50
【问题描述】:
我正在使用 AsyncStorage,我需要获取存储到“facoltà”中的值并将其保存到调用 this.setState 的“promessa”中。我写了那个代码:
constructor(props){
super(props)
AsyncStorage.setItem("facoltà","PROFS.json")
}
componentWillMount(){
AsyncStorage.getItem("facoltà").then((value)=>
{
console.log(value); // the console returns me PROFS.json so I thought it was working
this.setState({promessa:value})
}):
var dataObjects=require("../JsonLists/"+this.state.promessa) // but here this.state.promessa returns me null
}
问题是 this.state.promessa 返回“null”而不是“PROFS.json”,我不知道如何解决它。 提前感谢您的回答。
【问题讨论】:
-
您将无法通过这种方式导入文件:
require("../JsonLists/"+this.state.promessa)。您不能动态导入文件。查看this answer 了解原因。 -
谢谢,我修复了 Antoine Grandchamp 所说的 require 错误。然后我修复了 dataObject 问题,如您所说,将其放入 then 链中,谢谢。
标签: javascript react-native null setstate asyncstorage