可能的未处理的承诺拒绝(ID 0)类型错误this.setstate不是函数

时间:2019-01-06 08:48:06

标签: react-native promise icons

尝试获取图标时出现错误,请使用react-native-vector-icons库。我阅读了有关库的所有文档,并尝试了它们的示例,但是什么也没有。

class AuthScreen extends Component {
   constructor(props){
      super(props);
      this.state = {
          userIcon: "",
          lockIcon: ""
     }
  };
  render () {
     return (        
             <View style={styles.container}>
              <TextInput
                 placeholder="Usuario"
                 style={styles.InputContainer}
                 placeholderTextColor="#EEE"
                 inlineImageLeft={this.state.userIcon}
             />
               <TextInput
                 placeholder="Contraseña"
                 secureTextEntry={true}
                 style={styles.InputContainer}
                 placeholderTextColor="#EEE"
                 inlineImageLeft='search_icon'
                 inlineImageLeft={this.state.lockIcon}
              />             
              </View>
        ); 
    }
} 
Promise.all([
    Icon.getImageSource("md-person", 30),
    Icon.getImageSource("md-lock", 30)
]).then( sources => 
    this.setState({ 
        userIcon: sources[0],
        lockccccIcon: sources[1]  
    })
);

1 个答案:

答案 0 :(得分:1)

您需要在componentDidMount或在构造函数中与“ this”绑定的任何函数中兑现承诺。

componentDidMount(){
    Promise.all([
        Icon.getImageSource("md-person", 30),
        Icon.getImageSource("md-lock", 30)
    ]).then( sources => 
        this.setState({ 
            userIcon: sources[0],
            lockccccIcon: sources[1]  
        })
    );
}
相关问题