javascript - Display images on ReactJS -


i'm building app display images on react page. images have been uploaded onto local folder. coupled react app nodejs server , mongodb.

i want display img saved in folder. i've been trying use path of file don't think works img tag.

could clarify on that?

class showallphotos extends component {    constructor(props) {     super(props)      this.state = {       library: []     }   }    componentdidmount() {     axios.get('/all')     .then((response) => {       let arr = response.data;       console.log('arr', arr);       this.setstate({         library: arr       });       console.log("response data", arr);       console.log('library worked', response.status); // ex: 200     })     .catch(function (error) {       console.log(error);       console.log('error getting library', error.status);     });    }    render() {        let renderlibrary = () => {         return(           <div>             {this.state.library.map(function(library) {               return(                 <div classname="row" >                   <div classname="col-xs-6 col-md-6" id='card'>                     <img src={library.path} alt='some text'/>                   </div>                 </div>               );             })}           </div>         )       }      return (       <div classname="container">         <div>         {renderlibrary()}         </div>       </div>     );   } }  export default showallphotos; 

and here's route backend - checked on postmate , it's ok.

var express = require('express'); var router = express.router();  var photo = require('../model/photo');  router.get('/', function(req, res, next){   photo.find({}, function(err, photos){     if(err){       res.json({error:err});     }     res.json(photos);   }); });  module.exports = router; 

you need require image use in img tag. use -

<img src={require(library.path)} /> 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -