I would like to display an image in React, that is located in the src/images folder. This image has a PATH that is stored in the database and looks like this "../images/profilepic.jpg". I make my api call, get the PATH string and save it in a state called profilepic, so far this all works correctly. I then assign the state variable to a profilepic variable, which is correctly assigned when I "console.log()" it.
The issue is that I cannot show this image on the website when I use <img src{profilepic} alt="myprofilepic"/>. I have tried almost every method suggested in past questions but none work. I have tried to pass the state this.state.profilepic, with the string PATH assigned to it, to a separate component as a prop but i still run into the same issue. Using <img src=(require{profilepic}) produces an error as require will only take a string URL.
Can anyone point me in the direction of a tutorial where an image is stored as a URL/path string in a database and is called and used in React. Even if it isn't a complete project I would like to know how this can be done from the React front-end. If this is not the recommended way to deal with dynamic images in React would it then be more advisable to store the image as a BLOB in the database and then call the actual image itself in React instead of it's PATH?
The fetch call:
getUserdetails(){
const userid = this.state.userid;
axios.get(`http://localhost:5000/getuser/details?userid=${userid}`)
.then(json => {
if(json.data.success){
this.setState({
username: json.data.username,
email: json.data.email,
fullname: json.data.fullname,
company: json.data.company,
profilepic: json.data.propic
})
profilepic = this.state.profilepic
console.log(profilepic)
this.newprofile(profilepic)
}
})}
The Rendered output
return (
<div className="accout-page">
<div className="sidebar">
<Images profpic={profilepic} />
<p><input type="file" onChange={this.onchangeImage} /><button type="button" onClick={this.uploadimage}>Upload</button></p>
<p>Username: {username}</p>
<p>Full Name: {fullname}</p>
<p>Company: {company}</p>
<p>Email: {email}</p>
</div>
<div className="content">
<h1>Edit your details here</h1>
<div className="form-error-info">{updateMessage}</div>
<form>
<div className="form-group">
<label>Full Name:</label>
<input
type="text"
placeholder={fullname}
onChange={this.onNameChange} required/></div>
<div className="form-group">
<label>Company:</label>
<input
type="text"
placeholder={company}
onChange={this.onCompanyChange} required/></div>
<div className="form-group">
<label>Current Password:</label>
<input
type="password"
placeholder="enter current password"
onChange={this.onoPassChange} required/></div>
<div className="form-group">
<label>New Password:</label>
<input
type="password"
placeholder="enter new password"
onChange={this.onPassChange} required/></div>
<button className="btn btn-light" type="button" onClick={this.onSubmit}>Update</button>
</form>
</div>
</div>
)
The Images Component:
class Images extends React.Component{
render(){
const profpic = this.props.profpic;
console.log(profpic)
return(
<div>
<img src={profpic} alt="profile pic" width="100px" height="100px" />
</div>
)}}