Paulo Avila
Paulo Avila

Reputation: 321

activate async function on load screen

Im trying to create a users list with my api using a async function, but I dont know how to user it on load screen, can you help me

 export default class Dermatologistas extends Component{
  state ={
    errorMessage: null,
    users: []
  }
  getUserList = async () => {
    try {
      const response = await api.get('/auth/list');

      const { users } = response.data
      console.log(response.data)
      this.setState({ users });
     
    } catch (response) {
      this.setState({ errorMessage: response.data.error });
    }
  };
  
  
  render(){
    const users = this.state.users
    console.log(users)
    return(
      <View >

how you can see I was using a button to load everything, but i wanted to load when the screen loads

        <Button onPress={this.getUserList} title='carregar'/>
        {this.state.users.map(user => (
          <View key={user._id} style={{marginTop: 15, alignItems: 'center'}}>
            <Text>{user.title}</Text>
            <Text>{user.speciality}</Text>
            <Button   title = 'View Profile'onPress ={() => this.props.navigation.navigate('Profile')}/>
            </View>
        ))}
      </View>
    )
  }
}

Upvotes: 0

Views: 1692

Answers (1)

Mellet
Mellet

Reputation: 1286

componentDidMount() {
  this.getUserList();
}

Upvotes: 1

Related Questions