javascript - React Native 函数不返回

标签 javascript react-native

我的 React Native 应用程序中有一个页面 RepsPage,它根据 ID 数组呈现 ScrollView 。我将此数组传递给另一个函数 renderRep 并尝试返回数组中每个项目的 View ,但返回的只是空的。我尝试对其进行调试,似乎正在传递数据库中的对象,并且肯定有要显示的内容,但它只是没有使其成为返回函数的方式。我已经尝试了多次迭代(在 Firebase 查询中具有返回函数),但似乎没有任何效果。附代码:

export default class RepsPage extends Component {

renderRep(repID){
        var rep = new Firebase('https://ballot-app.firebaseio.com/congressPerson');       
        var nameView;
        var partyView;
        //entire function not executing? 
        rep.orderByChild("bioguideId").equalTo(repID).on("child_added", function(snapshot) {
            console.log(snapshot.val());

            nameView = <View style ={styles.tagContainer}>
                    <Text>{snapshot.val().name}</Text>
                    <Text>{snapshot.val().party}</Text>
               </View>
        });

        return (
            nameView
        );
}  

render(){ 
    var user = this.props.user;
    var reps = user.representatives;

    return (
        <ScrollView
            automaticallyAdjustContentInsets={false}
            scrollEventThrottle={200}
            style={styles.scrollView}
        >
                { reps.map(this.renderRep.bind(this)) }
        </ScrollView>
    );
}
}

最佳答案

您的 renderRep 方法应该设置状态而不是返回值。

类似这样的事情。请注意,我尝试编写此“盲目”代码:

export default class RepsPage extends Component {
    construct() {
        this.state = {
            snapshots: []
        }
    }

    componentDidMount(){
        this.fetchRep('someid');
    }

    fetchRep(repID) {
        var rep = new Firebase('https://ballot-app.firebaseio.com/congressPerson');
        var nameView;
        var partyView;
        //entire function not executing?

        rep.orderByChild("bioguideId").equalTo(repID).on("child_added", (snapshot)=> {
            console.log(snapshot.val());
            this.setState({snapshots: [].concat(this.state.snapshots, [snapshot.val()])});
        });
    }

    renderRep() {
        return this.state.snapshots.map((snapshot)=>
            <View style={styles.tagContainer}>
                <Text>{snapshot.name}</Text>
                <Text>{snapshot.party}</Text>
            </View>
        );
    }

    render() {
        var user = this.props.user;
        var reps = user.representatives;

        return (
            <ScrollView
                automaticallyAdjustContentInsets={false}
                scrollEventThrottle={200}
                style={styles.scrollView}
            >
                { this.renderRep.bind(this) }
            </ScrollView>
        );
    }
}

关于javascript - React Native 函数不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35884522/

相关文章:

javascript - 如何通过不同数组中的值来命名数组中的值?

javascript - 如何使用 jQuery 将 html 元素附加到鼠标?

javascript - JSON 对象作为键值对中的键

react-native - 如何在 TabNavigator 上锁定特定屏幕的方向

android - 安装 React Navigation 和手势处理程序后出错

java - 获取图片位置 - Phonegap 相机

javascript - lodash toNumber 和 parseInt 有什么区别?

android - 在 Realm 监听器回调中调用 setState 时,直到点击屏幕后 UI 才会更新

javascript - 如何在 react native 文本输入中获取新输入的文本?

android - React-Native:无法重新创建Android和iOS文件夹