Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
API call with React Native and axios

I'm new to React, what I'm trying to do is call my API with a GET request to change the status of an elevator to "Operational" and it's color status from red to green. The problem is that it doesnt seem to call the API and brings me back to my previous screen when I click CONFIRM.

the function for my call to the API looks like this

function ElevatorStatusScreen({navigation, route}) {
const { Elevator } = route.params;
const [elevatorStatus, setEleStatus] = React.useState(Elevator.status);
const [statusColor, setColorStatus] = React.useState('red');


// Call to the api to update the status of an elevator
const endtask = () => {
    axios.get(`https://apilink/api/Elevators/update/${Elevator.id}/Operational`)
    .then(response => {
        if(response.status == 200){
            Alert.alert('Status has been changed to Operational!')
            setColorStatus('green')
            setEleStatus('Operational');
        }
    })
}

return (
    <ImageBackground 
    style={styles.background}
    source={require('../assets/white.jpg')}
    >
        <Text style={styles.title}>Elevator: {Elevator.id}</Text>
        <Text style={{ color: statusColor }}>{elevatorStatus}</Text>          
        {elevatorStatus === 'Stopped' ? (
            <TouchableOpacity style={styles.endTask} onPress={() => endtask()}>
                <Text style={styles.endText}>END TASK</Text>
            </TouchableOpacity>
        )
        :
            <TouchableOpacity style={styles.confirm} onPress={() => {
                navigation.goBack()
            }}>
                <Text style={styles.endText}>CONFIRM</Text>
            </TouchableOpacity>
        }
    </ImageBackground>
);
}

Not sure what I'm doing wrong. This is my endpoint in a C# project coming from my elevators controller. (I tested it and it works)

[HttpGet("update/{id}/{status}")]
public async Task<dynamic> test(string status, long id)
{
    var elevator = await _context.elevators.FindAsync(id);
    
    elevator.Status = status;
    await _context.SaveChangesAsync();         

    return elevator;
}

the endpoint: //apilink/api/Elevators/update/1/Operational returns a JSON like this in my browser

{ "id":1, "status":"Operational", "columnId":1, "serialNumber":"666999990867" }

let me know if you need more code and thanks for your help.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem was that my elevatorStatus was 'offline' and NOT 'Stopped' in my database...sorry about that

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!