【发布时间】:2020-10-11 16:31:59
【问题描述】:
所以我正在制作一个 React Native 应用程序,我在 heroku 上设置了服务器和数据库。 使用 expo 在手机和我的电脑上的网络浏览器中查看。
在网络上,一切正常。
在手机上,我从屏幕截图中得到了错误。我猜是数据没有按时出现的问题,或者可能有不同的编写方式?
有没有办法在不使用 android studio 的情况下在手机上获取完整的应用程序?
这是与错误相关的代码:
<TextInput
style={styles.input}
placeholder='Search...'
onChange={(e) => { this.handleInput(e.target.value) }}
></TextInput>
handleInput = (value) => {
if (value !== this.state.insertionIngredient) {
this.setState({ ...this.state, insertionIngredient: value });
};
};
handleFindButton = () => {
this.setState({ ...this.state, isLoading: true });
const id = searchString(this.state.insertionLanguage, this.state.data,
this.state.insertionIngredient);
const result = getById(this.state.queryLanguage, this.state.data, id);
if (id === 'No such word in the database.' || result === 'No such word in the database.') {
this.setState({ ...this.state, queryIngredient: 'No such word in the database.' });
} else {
this.setState({ ...this.state, queryIngredient: result });
};
};
const searchString = (language, state, string) => {
let array;
if (language === 'english') {
array = state.english;
} else if (language === 'spanish') {
array = state.spanish;
} else if (language === 'german') {
array = state.german;
} else if (language === 'serbian') {
array = state.serbian;
};
string = string.toLowerCase();
let filter = escapeRegExp(string);
let regex = new RegExp("^" + filter, "i");
const word = array.filter(val => {
return val.word.match(regex)
});
if (word.length > 0) {
return word[0].id;
} else {
return 'No such word in the database';
}};
【问题讨论】:
-
由于可用的代码和快照有限,我只能怀疑错误是否与状态
insertionIngredient有关,具有不同的值。我建议插入几个日志语句来跟踪所有函数体中insertionIngredient的值。此外,不要将 args 传递给searchString(this.state.insertionLanguage, this.state.data, this.state.insertionIngredient).. 我建议,在没有 args 的情况下调用searchString()并从状态访问语言、状态、字符串值。 -
我可以尝试将代码带回主组件而不是导入它,但我认为这不会解决问题。
-
不,我的意思是,仅将代码保留在您的组件中,但可能会改变您布置逻辑的方式。您的代码块看起来是线性的,实际上并没有串行执行,而是所有行都是并行执行的。我的意思是,
state对insertionIngredient的价值可能与我们预期的不同。
标签: javascript android react-native expo