[英]Array of text from a JSON object inserted into <Text> elements via forEach/map not rendering in React Native
I'm making a Choose Your Own Adventure style game in React Native to learn how everything works.我正在用 React Native 制作一个选择你自己的冒险风格的游戏,以了解一切是如何运作的。 I figured it makes sense to keep the story in a separate JSON file and render it on one screen that updates depending on what arc the user is in.
我认为将故事保存在单独的 JSON 文件中并在一个屏幕上呈现它是有意义的,该屏幕根据用户所处的弧线进行更新。
Problem: when I write a forEach or map or other function to go through the array of paragraphs, the part of the screen it's supposed to be on is blank.问题:当我通过段落数组编写 forEach 或 map 或其他 function 到 go 时,屏幕的部分应该是空白的。 Text from other parts of the return area displays fine, but what is supposed to be displayed from the forEach does not.
返回区域其他部分的文本显示正常,但应该从 forEach 显示的内容却没有。 Here's my code:
这是我的代码:
const StoryDisplayScreen = props => {
const theStory = require('../data/TestStory.json')
const theArc = 'intro'
const storyText = () => {
theStory[theArc].story.forEach((paragraph, i) => {
<Text style={styles.body}>{paragraph}</Text>
})
}
return (
<View style={styles.screen}>
<ScrollView>
<View>
{storyText()}
</View>
<View style={styles.promptNextArcArea}>
<TouchableOpacity style={styles.promptNextArc}>
<Text style={styles.promptNextArcText}>What do you do?</Text>
</TouchableOpacity>
</View>
</ScrollView>
</View>
)
}
In case you're wondering the structure of the JSON file and its contents, I'm using this one to test with: https://github.com/gophercises/cyoa/blob/master/gopher.json如果您想知道 JSON 文件的结构及其内容,我将使用此文件进行测试: https://github.com/gophercises/cyoa/blob/master/gopher.Z466DEEC76ECDF35FCA546D
I've tried using map instead, tried putting things in Views, tried putting the forEach/map functions directly into the return section of the code, tried console.log-ing to confirm that my functions are working properly (which they appear to be), and... yeah, I'm at a loss.我尝试使用 map 代替,尝试将内容放入视图中,尝试将 forEach/map 函数直接放入代码的返回部分,尝试 console.log-ing 以确认我的函数工作正常(它们似乎是),而且......是的,我很茫然。
Any suggestions?有什么建议么?
Consider using map
instead of forEach
.考虑使用
map
而不是forEach
。
Full Working Example: Expo Snack完整工作示例: Expo Snack
import * as React from 'react';
import {
Text,
View,
StyleSheet,
ScrollView,
TouchableOpacity,
} from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';
const StoryDisplayScreen = (props) => {
const theStory = require('./data/TestStory.json');
const theArc = 'intro';
const storyText = () => {
return theStory[theArc].story.map((paragraph, i) => (
<Card style={{margin: 5, padding:5}}>
<Text style={styles.body}>{paragraph}</Text>
</Card>
));
};
return (
<View style={styles.screen}>
<ScrollView>
<View>{storyText()}</View>
<View style={styles.promptNextArcArea}>
<TouchableOpacity style={styles.promptNextArc}>
<Text style={styles.promptNextArcText}>What do you do?</Text>
</TouchableOpacity>
</View>
</ScrollView>
</View>
);
};
export default StoryDisplayScreen;
const styles = StyleSheet.create({
screen: {
flex: 1,
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
});
firstly, if you want your sort is a function, it must return something, just like the render
method首先,如果你希望你的排序是 function,它必须返回一些东西,就像
render
方法一样
const sort = () => {
return (<View></View>)
}
you also can make the sort is a view, use it as a variable.您还可以将排序设置为视图,将其用作变量。 like that, both they are can works.
像这样,他们都可以工作。
const sort = (<View></View>)
but the sort must have something return.但是排序必须有一些回报。 your way use
for each
and map
dont return any.您的方式使用
for each
map
不返回任何。 there are two ways can do that.有两种方法可以做到这一点。
const storyText = () => {
//,first way,define a array, push it in the array
let storyViews= []
theStory[theArc].story.forEach((paragraph, i) => {
sortyviews.push(<Text style={styles.body}>{paragraph}</Text>)
})
return soortView;
}
the second way is to return directly a array第二种方式是直接返回一个数组
const storyText = () => {
//,first way,define a array, push it in the array
let storyViews= []
storyViews = theStory[theArc].story.map((paragraph, i) => {
return (<Text style={styles.body}>{paragraph}</Text>)
})
return soortView;
}
for more about the array operate to see the doc有关数组的更多信息,请查看文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.