I am trying to render an image on my React Web App, but the image is broken. However, when I do a console.log of the file's path, I see the following which seems to be correct.
We are using url-loader and file-loader with webpack, and am importing the file path directly in the import statements as required. I have tried adding required ('imageFilePath.png'), but that did not work either.
Currently, the image is placed directly in the src folder along with App.js.
Here is my App.js:
import React, { Component } from 'react'
import './App.css'
import {Tweet} from 'react-twitter-widgets';
import logoFinal from './logoFinal.png';
class App extends Component {
render () {
const { error, isLoaded} = this.state;
if (error){
return <div> Error: {error.message}</div>
} else if (!isLoaded){
return <div>Loading...</div>
} else{
return (
<div className="container">
<nav className="navbar navbar-expand-lg navbar-light fixed-top">
<div className="container">
<a className="navbar-brand pull-left" href="/home">
<div>
<img src={require('./logoFinal.png')} width='100' margintop='-7' /></div></a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarNavAltMarkup">
<div className="navbar-nav">
<a className="nav-item nav-link active" href="#">Home <span className="sr-only">(current)</span></a>
<a className="nav-item nav-link" href="#">By State</a>
</div>
</div>
</div>
</nav>
And then in my webpack.config.js I have the following in the module section:
{
test: /\.(png|jp(e*)g|svg)$/,
use: [{
loader: 'url-loader',
options: {
limit: 8000, // Convert images < 8kb to base64 strings
name: 'images/[hash]-[name].[ext]'
}
}]
},
{
// "oneOf" will traverse all following loaders until one will
// match the requirements. When no loader matches it will fall
// back to the "file" loader at the end of the loader list.
oneOf: [
// "url" loader works like "file" loader except that it embeds assets
// smaller than specified limit in bytes as data URLs to avoid requests.
// A missing `test` is equivalent to a match.
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
},
},
Welcome to StackOverflow!
With this line, you have imported the image.
import logoFinal from './logoFinal.png';
The logoFinal variable will be the actual image-path, which will be generated by webpack. So you can use it in the <img /> tag as the src attribute.
<img src={logoFinal} />
You webpack config looks weird, because you have defined two rules for images. The later one with require.resolve('url-loader') seams to be correct.
I'm working with Natalie. I've removed the extra url-loader in the web pack and kept the second. I've tried the suggestions and the image still renders as broken. I see the img in the html as this: http://localhost:3000static/media/logoFinal.fb830421.png
I've also attempted to use file-loader.
Related
This is my Header component
import React from "react";
function Header() {
return (
<div classname = "header">
<nav classname = "navbar navbar-expand-sm navbar-dark bg-dark fixed-top">
<div classname = "container">
<button classname = "navbar-toggler" data-toggle="collapse" data-target = "#Navbar">
<span classname = "navbar-toggler-icon"></span>
</button>
<h3>Portfolio</h3>
<div classname="btn-group">
<button type="button" classname="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
My networking
</button>
<div classname="dropdown-menu">
<a classname="dropdown-item" href="https://docs.google.com/document/d/1iJ1v7HyfodCWvg5OSQIXbUATrBM9mPwdqeIzKuld1qs/edit">Resume</a>
<a classname="dropdown-item" href="https://www.linkedin.com/in/tyler-goodman-39b289195/">LinkedIn</a>
<a classname="dropdown-item" href="https://github.com/KawaiiSlave">Github</a>
</div>
</div>
<div classname = "collapse navbar-collapse" id = "Navbar">
<ul classname = "navbar-nav ml-auto">
<li classname = "nav-item">About Me</li>
<li classname = "nav-item">Skills</li>
<li classname = "nav-item">My Projects</li>
<li classname = "nav-item">My Contact Info</li>
</ul>
</div>
</div>
</nav>
</div>
);
}
export default Header;
Here is my app.js file
import React from 'react';
import './App.css';
import Header from "./components/Header/header";
import About from './components/About/about';
function App() {
return (
<div className="App">
<Header/>
<About/>
</div>
);
}
export default App;
Ive got my stuff to render, but Im confused on where I should put my css. Does it go into the app.css file provided, or is it better off inside the living component, as that components css? If someone could give an example with their own css that would help tremendously. Im just kinda overwhelmed with react starting out. Thank you!
If you want to create specific CSS for your different components, you could use modules. I tend to make 'CSS modules' when I am creating a component I will use often, for instance a card module.
If you have a Card.js component, for instance, you might create a file called card.module.css.
At the top of Card.js, I would then import this using
import style from './card.module.css';
Within Card.js you can then add a className to an element using a JSX expression which accesses the imported style like so
<div className={style.header}>
Within the card.module.css file, you can then write css for .header
.header {
// your CSS here
}
To re-iterate, your Card.js file will access .header through the imported 'style'. If you were to add more CSS in your module, such as
.wrapper {
// your CSS here
}
You could then too access that in your Card.js with
className={style.wrapper}
I have a website built with ReactJS and there are some files (PDF,DOC etc) that I want to let visitors download.
But simple href tag doesn't start the download, instead refresh the page.
<Button
href="src/assets/files/exampleDoc.pdf"
color="transparent"
target="_blank"
className={classes.navLink}
>
Why the href tag doesn't work in reactJS to download files?
How to download files with ReactJs using buttons?
The following approach worked for me.
import ExampleDoc from '......src/assets/files/exampleDoc.pdf'
<a href={ExampleDoc} download="MyExampleDoc" target='_blank'>
<Button className={classes.navLink}>My Example Doc</Button>
</a>
If your file is on the frontend side and you are not storing it in backend you can do trigger the download by using the <a> tag in HTML. Try the following
Download file
Try this:
const DownloadButton = props => {
const downloadFile = () => {
window.location.href = "https://yoursite.com/src/assets/files/exampleDoc.pdf"
}
return (
<button onClick={downloadFile} />
)
}
export default DownloadButton;
for you first question you can check this it working
<Button
href="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxISEBUSEhAVFRUVFQ8VFRUVEA8VEBUPFRUWFhUVFRUYHSggGBolHRUVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OFxAQGi0eHx8tLS0tLS0tKy0vKy0tLS0tLS0tLS0tLy0tLS0tLSstLS0tLS0tLS0tKy0rLS0rLS0rK//AABEIALcBEwMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAACAwABBAUGB//EADcQAAICAQIDBQYEBgIDAAAAAAABAhEDBCESMUFRYXGBkQUTFCKhsULB0fAyUmJy4fEGojOCkv/EABoBAAMBAQEBAAAAAAAAAAAAAAABAgMEBQb/xAApEQACAgICAgEEAAcAAAAAAAAAAQIRAyESMQQTQRQiUWEyUoGh4fDx/9oADAMBAAIRAxEAPw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i9sblTVLiTvq4/ejPknXVPw2Q5YsnSQvJhydil5R+gmVGvyiY5x2bf/a16MbGre8afck/VGKHOq8rr7h6i10i+9WvIVluG6Hww47q732+bc0xtcvmXe1Zy45K5wj9f2g56p38jY00KWKT/wAm3LOMueO//ltCp6bHfKUb8KNGJNreUW/7Rc8nNOVd9tL6oZlFtaRqxSiklxRfml9Bvgr9DEpQa3kn5K/p+hMS/kUq7nHh+5XIyeM1vJ/TL0v7FQyJ9GvFNEWSXVet/lYy33eo0ZtV/wBFScr2jfmQZb7PsQAv9HmuZRRDE9goNPYhBAVKikQgwCUXe/3Df8SaVLvdkIIQtLmFCiiDGNUu7oA4r92QgElSxdbLtpbkIJjRXEWns1+0yEAdBaXJwyT/AHXU7yiQhpA4vLXTL4RE1JbKEa8ef0IQpnLGVMyT0bVt1XOq28LW4yGqpbwddfmTLIQ9PR0xfsX3FRzRlb95Lfu/wUsi4qTTffFqyECzTglY3PwWlJfcHEsfJLv6/voQgzNR+3sd7u18rvrvfLxF5Fwq+Bd/L8yEKoxUnyoPHNtcWy7qELJFv5oxve+f2LISzWKVsv4Xs4fJNP1Bek4VtOS89iyA0R7JXRax5NuHLfiv8BvPOC+aKfen+RCDrVhF8pcWiL2lHsf0IQhHNnV9LjP/2Q=="
color="transparent"
target="_blank"
download>Download
</Button>
you can have a look hover here https://react.rocks/example/downloadbutton
function makeFile() {
return {
mime: 'text/plain',
filename: 'myexportedfile.txt',
contents: 'all of the exports',
}
}
<DownloadButton
className='waves-effect waves-light btn'
genFile={makeFile}/>
I think, this will work perfectly fine:-
<a href="assets/img/Rishabh-Singh-Resume.pdf" download="Rishabh's Resume" target='_blank'>
<button type="button" class="btn btn-success btn-lg btn-block">Download Resume</button>
</a>
I have an issue with the <a href="#"> in my AngularJS app.
My main issue is on the:
<em class="fa fa-bars"></em>
navigation.html -> this is a template of navigation directive.
<nav class="sidebar col-xs-12 col-sm-4 col-lg-3 col-xl-2 bg-faded sidebar-style-1">
<h1 class="site-title"><em class="fa fa-rocket"></em> Brand.name</h1>
<em class="fa fa-bars"></em>
<ul class="nav nav-pills flex-column sidebar-nav">
<li class="nav-item"><a class="nav-link active" href="index.html"><em class="fa fa-dashboard"></em> Dashboard <span class="sr-only">(current)</span></a></li>
</ul>
<em class="fa fa-power-off"></em> Signout</nav>
index.html
<body ng-app="test" class="body-bg" ng-style="bgimg" ui-view>
</body>
app.js
var app = angular.module("test", ["ui.router");
app.config(function($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise('/');
$stateProvider
// login route
.state("login", {
url:"/",
controller: "LoginController",
templateUrl: "pages/login/login.html"
})
// admin route
.state("admin", {
url:"/admin",
controller: "AdminController",
templateUrl: "pages/admin/admin.html"
})
And currently I am doing a SPA routing using ui-sref of ui-router.
The problem is everytime I click the button, I get redirected to the default state $urlRouterProvider.otherwise('/');.
How I can do something similar to that HTML href="#" with AngularJS ui-sref?
Or is there other ways?
from the code in the question I can say that, the normal usage of href will result in redirection so you need to use data-target, can you please try this?
<a data-target="#menu-toggle" data-toggle="collapse" class="btn btn-default"><em class="fa fa-bars"></em></a>
JSFiddle: here
if you have jQuery (which you probably do)
inside your head tag...
<script>
$(document).ready(function(){
$('[href="#menu-toggle"]').click(function(event){
event.preventDefault();
})
});
</script>
Basically keeps the link from routing via window navigation.
You can easily test by pasting this code into your browser console..
$('[href="#menu-toggle"]').click(function(event){
event.preventDefault();
})
You can use a combination of href and ng-click to solve your problem.
<em class="fa fa-power-off"></em> Signout</nav>
Where signOut() is a function on your controller.
1.Just remove the href tag completely from your anchor tag. It's still a perfectly valid tag without it. (or) href="javascript:void(0);"
for your reference: Angular-UI-Router: should i avoid href with ui-router?
I am creating a Twitter Tweet Analysis web page. I want to localize the page based on the user selection. On the header I am providing a nav bar : Country which lists different locales. Based on the country selected by the user, the entire webpage should get translated.
Here is my HTML header:
<header>
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<a class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="navbar-brand" href="#">Twitter Analysis</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav ">
<li class="dropdown">
Country
<ul class="dropdown-menu">
<li>United States</li>
<li>China</li>
</ul>
</li>
</ul>
</div>
</div>
</header>
Any idea how I should implement this? Please advice. Thanks
You can use i18n-2 module for this.
First, you need to install i18n-2 using npm
$ npm install --save i18n-2
After that, add these config line into your app.js file. Remember to add it after you have loaded the cookieParser.
var cookieParser = require('cookie-parser');
app.use(cookieParser('your secret here')); // put the config after this line
i18n.expressBind(app, {
// setup some locales - other locales default to vi silently
locales: ['vi', 'en'],
// set the default locale
defaultLocale: 'vi',
// set the cookie name
cookieName: 'locale'
});
// set up the middleware
app.use(function(req, res, next) {
req.i18n.setLocaleFromQuery();
req.i18n.setLocaleFromCookie();
next();
});
The i18n object will now reside within the request object of each request. The above config also allows the locale to be set from query string or from cookie. For example, the mysite.com/?lang=en will automatically set the locale to en. To use the i18n object, simply use the __ function
function handlerFunc1(req, res){
res.render('index', { title: req.i18n.__("hello") });
}
Or if you want to use it in your view, simply use __ again
<h1>
<%= __("hello") %>
</h1>
i18n-2 will then look up the key hello in the locale files (by default located in locales/en.js and locales/vi.js). If the keys or the files is not exist yet, it will then create those files and keys automatically for you so you don’t have to worry about the errors. You can then open those files and edit the values already there or add your new one. The file syntax is just normal JSON syntax.
Note: you cannot use // for comments.
{
"hello": "Hello",
"title": "title",
}
To change the language, you can set it directly using setLocale(locale) function. Beside that, you can set the cookie locale value for the browser to remember the current language for the next access.
function handlerFunc(req, res){
// you can set it directly like this
req.i18n.setLocale('vi');
// or set it via the cookie
res.cookie('locale', 'vi');
req.i18n.setLocaleFromCookie();
// redirect back
res.redirect('back');
};
Source - link
I am following MVA intro to Angularjs. I have two questions. First; is there any better tutorial website that you would suggest.
Second is using ui-router. I have been looking at it for hours; went looking for the answer, and just cant figure out what could possibly be wrong. Everything looks like the tutorial; the library came off of it; and everything was working in till I switched ng-route with ui-router(and all the href with ui-sref). I think the problem is in the mainApp module since the page is white (when I have had problems with the mainPageModule the navbar would show up but it would have {{event.something[0]}} listed across the top). This is driving me crazy.
index.html
<div ng-include src="'app/event/directives/header.html'"></div>
<div class="container">
<ui-view></ui-view>
</div>
<script src="./app/angular.min.js"></script>
<script src="./app/mainApp.js"></script>
<script src="./app/ui.router.js"></script>
<script src="./app/event/mainpageModule.js"></script>
<script type="text/javascript">
console.log("angular object",angular);
</script>
</body>
mainApp.js
angular.module('mainApp', ['mainPageModule','ui.router'])
.config(['$urlRouterProvider','$stateProvider',
function ($urlRouterProvider, $stateProvider) {
$stateProvider
.state("home", {
url: "/home",
templateUrl: 'main.html'
})
.state("area", {
url: "/area",
templateUrl: "area.html"
})
.state("personal", {
url: "/personal"
templateUrl: "personal.html"
})
.state("contact", {
url: "/contact"
templateUrl: "contact.html"
})
$urlRouterProvider.otherwise('','/home')
}])
.run([function () {
/* Run is when the app gets kicked off*/
console.log("Run hook");
}])
mainPageModule.js
(function(){
angular.module('mainPageModule', [])
.factory('SiteName', [function () {
return {
title:"Website Name"
};
}])
.config([function () {
console.log("Event Module:: config");
}])
.run([function () {
console.log("Event Module::running");
}])
.controller('navigationbarController', ['$scope', 'SiteName',function ($scope,SiteName)
{ this.menu=[
{
name:"Website Name",
href:"home.html"
},
{
name:"Your Area",
href:"area.html"
},
{
name:"Personal",
href:"personal.html"
},
{
name:"Contact",
href:"contact.html"
}
]
this.index = 0;
this.setIndex=function(val)
{
this.index = val;
console.log("called")
}
this.getIndex=function(){
return(this.index);
}
}])
})();
header.html
<nav ng-controller="navigationbarController as event" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">{{event.title}}</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li ng-click="setIndex(0)" ng-class="(index==0) ? 'active' : ''">
<a ui.sref="home">{{event.menu[0].name}}</a>
</li>
<li ng-click="setIndex(1)" ng-class="(index==1) ? 'active' : ''">
<a ui.sref="area">{{event.menu[1].name}}</a>
</li>
<li ng-click="setIndex(1)" ng-class="(index==1) ? 'active' : ''">
<a ui.sref="personal">{{event.menu[2].name}}</a>
</li>
<li ng-click="setIndex(1)" ng-class="(index==1) ? 'active' : ''">
<a ui.sref="contact">{{event.menu[3].name}}</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
As suggested by #TOM you need to load angular-ui-router right after angular.js has loaded thereafter you could load mainApp.js.
You need to replace your all ui-sref directive at all places.
ui-sref="home" instead of ui.sref="home"
For anybody else who has this problem, the fundamental flaw was the order of the scripts at the bottom of the page. It matters. Second, my script mistake was not including comma's after the url's in the mainApp. Moving forward, I replaced all the ui.sref's with ui-sref's and it did work.
Thanks for your help, pankajparkar and Tom was great. Anyone else who is starting out and run's into thisproblem, check the console, it will tell you whats wrong