0

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>
    )
  }
}
2
  • 1
    Take a look at componentDidMount. It allows you to do things when the component is loaded. Commented Feb 10, 2021 at 18:21
  • @Mellet yeah, but i dont know how to use it Commented Feb 10, 2021 at 20:05

1 Answer 1

1
componentDidMount() {
  this.getUserList();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.