首页 > 解决方案 > 当数据值为空时,React Native 下拉菜单不起作用

问题描述

触发操作创建后,我试图用来自 Reducer 的对象填充下拉组件。我试过了,但我做不到。

import React, { Component } from 'react';
import { View } from 'react-native'
import {connect} from 'react-redux';
import { Dropdown } from 'react-native-material-dropdown';
import { recuperarDisciplinas } from '../actions/AutenticacaoActions';

class cadastroMateria extends Component {

componentDidMount(){

 const { disciplina } = this.props;   
 this.props.recuperarDisciplinas({ disciplina }) 
}

render() {
   return (

  <View>

// First it hits here once before going to componentDidMount and at that moment as the 
// 'discipline' is still empty, the error dropdown 

    <Dropdown label="Disciplinas" data={this.props.disciplina } />
  </View>       
  );
}
}

const mapStateToProps = state => (
{
    disciplina: state.AutenticacaoReducer.disciplina,
}
);

export default connect( mapStateToProps, { recuperarDisciplinas })(cadastroMateria);

我研究了下拉菜单是否有可能至少第一次接受 null ,这样当“纪律”出现时,可以稍后填写,但我不能。我尝试了其他几件事,但我不知道如何解决这个问题。谁能帮帮我谢谢

标签: reactjsfirebasereact-nativereduxdropdown

解决方案


如果他从firebase带来我想要的对象,我的困难是因为他是异步的,他在下拉列表中给出了一个错误,因为“数据”是空的。我也尝试过这种方式,但错误仍然存​​在,其余的,这是正确的。

_recuperarDisciplinas(){

 const { disciplina } = this.props;

 this.props.recuperarDisciplinas({ disciplina }) 

/*this 'dropdown.disciplinas' at the beginning comes as false, after it passes 
through the firebase function I make the action turn it to true */

 if(this.props.dropdown_disciplinas){
    return <Dropdown label="Disciplinas" data={this.props.disciplina } />
 }
}

render() {
   return (

  <View>
{this._recuperarDisciplinas()}
  </View>

推荐阅读