I am making a function of API and calling it use Effect and keeping this all in use state but I am getting a blank space!
function api() {
var requestOptions = {
method: 'GET',
redirect: 'follow',
};
fetch('https://simple-books-api.glitch.me/books/2', requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
// const [count, setCount] = useState(0);
}
const [count, setCount] = useState(api);
const [modalVisible, setModalVisible] = useState(false);
const [item, setitem] = useState();
const [message, setMessage] = useState(api);
useEffect(() => {
api();
});
I want to get results of API.
4
Answers
There is a few mistakes with your code. Here is how you should implement it :
Like this,
message
andcount
states will have datas.You can display it in your return statement.
For example :
You forgot to enter
[]
prop for useEffect and set state value after fetching data. You can try the below code:Replace your API in the below code:
Here is the simple fetch API structure in React Native functional component.