I have this simple file upload button that I got from antd ant design documentation:
<Upload>
<Button
icon={<UploadOutlined />}
className="upload-btn"
>
Upload a file
</Button>
</Upload>
Every time I upload a file I get this error in the console log:
I don't want it to make a post request when I upload the file, I have a submit button for that.
You can do it by returning false from beforeUpload prop, like this:
<Upload beforeUpload={()=> {
/* update state here */
return false; }}>
<Button icon={<UploadOutlined />}>Select File</Button>
</Upload>
obviously in this manner you have to define a state, and store files in the state to send it to server manually. Here is an example to implement this logic.
I have a react component
const Header = () => {
return(
<div role="button" className="user-logout" onClick={logoutUser}>
<i className="fas fa-sign-out-alt user--nav--icon"></i>
Logout
</div>
)}
In the above code onClick logout the user gets logged out
const logoutUser = () => {
dispatch(logout());
history.push('/login');
};
but I want the user to get a message popup onClcik in a div with conforming logout or cancel
and then on confirming logout it should logout the user
You can pass a GET Parameter to the login page like:
history.push('/login?logout=1');
Then if the login page has this parameter, render your message.
If you don't want to use a parameter, you will have to look for another solution either with react context or react navigation
hey guys i am learning react js and I have an update form to update book info. I am using django rest api for endpoints. I have a working form where I can upload files and do all those stuffs but I am not able to show the image which is already there in the template, Here I have a book cover image, which is already there in the database, it should be showing in the front-end and when I change the image, the new one should show, how can I add that feature here, I tried <img src={formData.book_cover} and consoling out this is showing the url, but the image isn't getting displayed.
From the network tab,
The problem I think is
Request URL:http://localhost:3000/media/book/book_sample/pride_in_nat.png
request url since the image gets displayed if the url is localhost:8000 instead of localhost:3000 as it is where the django server backend runs. So, how can I change that?
This is the code.
import React from "react";
function BookInfoForm() {
const initialFormData = Object.freeze({
id: '',
book_cover: '',
book_name: '',
book_summary: '',
});
const [formData, updateFormData] = useState(initialFormData);
const [image, setImage] = useState(null);
const { register, handleSubmit, control, errors } = useForm();
useEffect(() => {
axiosInstance.get('api/books/info/update/').then((res) => {
updateFormData({
...formData,
['book_cover']: res.data.book_cover,
['book_name']: res.data.book_name,
['book_summary']: res.data.book_summary,
});
});
}, [updateFormData]);
const handleChange = (e) => {
if (e.target.name === 'image') {
setImage({
image: e.target.files,
});
// console.log(e.target.files);
}
updateFormData({
...formData,
// Trimming any whitespace
[e.target.name]: e.target.value
});
};
const onSubmit = (data) =>{
let formData = new FormData();
formData.append('user', user.id),
formData.append('book_cover', data.image[0]),
formData.append('book_name', data.book_name),
formData.append('book_summary', data.book_summary),
axiosInstance.put('api/books/info/update/', formData),
}
return (
<>
<form className={classes.form} noValidate onSubmit={handleSubmit(onSubmit)}>
<Grid container spacing={2}>
<Grid item xs={6}>
{/* Show existing book cover and change when new one is uploaded */}
<img src={formData.store_logo} alt="" />
<label htmlFor="book-cover">
<input
accept="image/*"
className={classes.input}
id="book-cover"
onChange={handleChange}
name="image"
type="file"
ref={register}
/>
Book Cover
<IconButton color="primary" component="span">
<PhotoCamera />
</IconButton>
</label>
</Grid>
<Grid item xs={12}>
<TextField
variant="outlined"
required
fullWidth
id="book_name"
label="Book Name"
name="book_name"
autoComplete="book_name"
value={formData.book_name}
onChange={handleChange}
inputRef={register({maxLength: 30})}
rows={1}
/>
</Grid>
<Grid item xs={12}>
<TextField
variant="outlined"
required
fullWidth
id="book_summary"
label="Book Summary"
name="book_summary"
autoComplete="book_summary"
value={formData.book_summary}
onChange={handleChange}
inputRef={register({maxLength: 1000})}
multiline
rows={3}
/>
</Grid>
</Grid>
<Button
type="submit"
fullWidth
variant="contained"
color="primary"
className={classes.submit}
>
Update
</Button>
</form>
</>
)
}
You might want to take a look at one of my answers on Why React needs webpack-dev-server to run?
As your frontend is running at localhost:3000 and you are providing a relative path to the img tag, the browser is assuming that the image is at localhost:3000.
Whenever your backend host is different than the frontend host, you have to provide a complete URL to the resource i.e., origin(http://localhost:8000) + path to the resource(/book/book_sample/pride_in_nat.png)
As you are storing the path to the resource in your database, just append the origin while giving it to the img tag.
<img src={`http://localhost:8000/${formData.store_logo}`} />
Suggestion
A better approach is to use .env files and load them according to your development or production environment
<img src={`${process.env.IMAGE_STORE_ORIGIN}${formData.store_logo}`} />
And in your .env file or .env.development file, you can add the entry for where your images are stored
In your .env file:
IMAGE_STORE_ORIGIN=http://localhost:8000/
So, when you want to change your backend server origin, you can just change it in one location and it is used inside your entire app instead of changing it manually every time you want to use a new server address.
Take a look at dotenv and dotenv-expand
I hope this should clarify your "why" and "what".
I would advise this as being the best solution and I highly would recommend this even though I have not worked heavily with Django.
I know with Django you can store files in media files as configured in your settings.py, as good and convenient that is I guess but the best solution in my understanding for managing files is dealing with a 3rd party like cloudinary(Very common when working with Node using 3rd party software's to manage files)
So the flow behind:
Make an account on cloudinary(Do not worry it's completely free but can upgrade)
When you want to save a file to DB you first interact with cloudinary with Django.
Once Django is done cloudinary Api will bring back a url of your image.
Then what you only have to do now is now save that link given in your database
Now with your Django Model
In the place where you had image as a "FileField" now you can safely convert to "CharField" or whatever that means text/string in django kind of forgot
Then now in your React app that should work
Cloudinary SDK docs for Django
https://cloudinary.com/documentation/django_integration
You can also look up some example on YouTube for clarity if docs are not clear
https://youtu.be/1T6G7Znrbfg
I am working on creating a dialer in React Native. For dialing a call I was using expo-linking but that only opened default phone dialer. After researching here I found about about react-native-immediate-phone-call and tried it but I get no results. I installed it via npm, ejected project from expo a ran the app on a virtual phone but after I press the dial button I get an alert (so I now the function is working), but no dialing begins whatsoever.
<Pressable onPress={this.pressButton}>
<FontAwesomeIcon icon={faPhone} color={colorName} size={35} />
</Pressable>
pressButton = () => {
this.setState({
callValue: (call = !call)
})
Alert.alert(number)
RNImmediatePhoneCall.immediatePhoneCall(number);
}
Could anyone please tell me why this is not working? Thank you.
check your phone permission for call :
In the AndroidManifest.xml file of your android studio project add:
<uses-permission android:name="android.permission.CALL_PHONE" />
So here is what I wish to make it work, I am using a webview to load zara site, their login form email element you can inspect here https://www.zara.com/uk/en/logon <input id="email" name="email" type="text" value="" autocomplete="off"
and I wish when my app webview lands on zara login page, react native app can auto fill the login email and password field.
Below is my attampt:
import React from 'react';
import { StyleSheet } from 'react-native';
import { WebView } from 'react-native-webview';
export default function ZaraWebView() {
return (
<WebView
source={{uri: 'https://www.zara.com/uk'}}
injectedJavaScript={`(function(){document.getElementById('email').value = 'test#test.com';}
());`}
style={{flex: 1, marginTop: 30}}
/>
);
}
But it does not seem working, any example suggestions?
Thanks
I had something similar, and when i removed the style from the Webview component it worked. I wrapped the Webview component in a View component, and applied the same style to that and it worked as expected.