javascript - 无法在React Native渲染方法中访问 map 函数内部的类函数

标签 javascript react-native

import React from 'react';
import {
    StyleSheet,
    Text,
    View,
    ScrollView,
} from 'react-native';

import { Content, Card, CardItem, Body, Icon, Image, Right, Left, Thumbnail, Button } from 'native-base';
import  HTMLView from 'react-native-htmlview'

import styles from '../components/CustomStyles'
import htmlstyles from '../styles/htmlstyles'

import TimeAgo from 'react-native-timeago'


export default class QuestionsData extends React.Component
{
    constructor(props)
    {
        super(props);
        this.state =
            {
                navigation: null
            }
    }

    componentDidMount()
    {
        this.state.navigation = this.props.navigation;
    }

    viewExtraFile = () =>{
        this.props.navigation.navigate("ExtraFile");
    };

    render() {

        let questions = this.props.data.map(function (questionData, index)
        {
            let  extra_file = "";
            if(questionData.extra_file_details.file_name !== undefined)
            {
                extra_file = (<CardItem>
                    <Button transparent onPress={()=> this.viewExtraFile()}> <Text style={styles.linkText}>{questionData.extra_file_details.file_name} </Text></Button>
                </CardItem>);
            }
            return (
                <Card transparent style={styles.card}>
                    <CardItem>
                        <Left>
                            <Thumbnail source={{uri: questionData.poster_image_url}} />
                            <Body>
                            <Text style={styles.username}>{questionData.poster_name}</Text>
                            <Text note style={styles.category}>{questionData.section_name}</Text>

                            </Body>
                        </Left>
                    </CardItem>
                    <CardItem content>
                        <HTMLView stylesheet={htmlstyles}
                            value={questionData.question }/>
                    </CardItem>
                    {extra_file}
                    <CardItem>
                        <Left>
                            <Button transparent>
                                <Icon name="thumbs-up" />
                                <Text>{questionData.num_applause} Applause</Text>
                            </Button>
                        </Left>
                        <Body>
                        <Button transparent>
                            <Icon  name="chatbubbles" />
                            <Text>{questionData.num_answers} Answers</Text>
                        </Button>
                        </Body>
                        <Right>
                            <Button transparent>
                                <Icon name='time'/>
                                <Text><TimeAgo time={questionData.time}/></Text>
                            </Button>

                        </Right>
                    </CardItem>
                </Card>
            )
        });
        return (

            <Content>
                {questions}
            </Content>
        );
    }


}

我无法访问 viewExtraFile 方法,请有人帮助我如何做到这一点

最佳答案

改变

this.props.data.map(function (questionData, index)
{

this.props.data.map((questionData, index) => {

否则map内部的this将不会引用 map 外部的this

关于javascript - 无法在React Native渲染方法中访问 map 函数内部的类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49825334/

相关文章:

React-Native 列表不滚动

javascript - 如何在react-native中导出所有项目的const

azure - react native azure 通知中心

javascript - jQuery:如何通过检查子元素来选择元素

javascript - JSZip 在 IE11 中使用 Promise.all 加载多个文件

javascript - 如何为我的任务实现实现 Promise.all 的等价物?

react-native - 没有 .babelrc 的 Jest 错误 使用它会出现 React Native 错误

javascript - 意外的标识符修复

javascript - 如何使输入值成为全局变量?

javascript - 如何在 native react 中从子组件更改父组件的状态?