React doesn't show component in Laravel 5.8 blade - reactjs

I'm using a basic React installation on Laravel 5.8. My blade hoolaMundo.blade.php identifies the file css/app.css and js/app.js, but it doesn't show anything in the browser.
holaMundo
<html>
<head>
<link href="/css/app.css" rel="stylesheet">
</head>
<body>
<div id="Example"></div>
</body>
</html>
<script src="/js/app.js"></script>
Component
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Main from "../../assets/js/components/Example";
class Example extends Component {
render() {
return (
<div className="container">
<div className="row justify-content-center">
<div className="col-md-8">
<div className="card">
<div className="card-header">Example Component</div>
<div className="card-body">I'm an example component!</div>
</div>
</div>
</div>
</div>
);
}
}
export default Example;
if (document.getElementById('Example')) {
ReactDOM.render(<Example />, document.getElementById('Example'));
}
Web Result
extra information
node -v :14.7.0
php -v : 7.3.5
npm -v: 4.14.7
Done
npm install
npm dev run

I would usually use the comment section, due to my insufficent reputation I'll need to type a big answere.
thing, it would be greate to see your mix file? Did you use .react? https://laravel.com/docs/5.8/mix#react
Laravel does sometimes behave odd, no ofense I love it, but I would recommend using the asset function to refrence your styles scripts etc. Then you make sure everything is send out properly.
I would put the script tag within the html tag.
it would be great to know how you installed react? Did you use larvel or node way?
How I do it.
I usually cd into my resource/js folder and install it via
npm i create-react-app myapp
Then I go into laravel mix and paste in:
.react('resources/js/myapp/index.js', 'public/js/myapp.js');
If you cannot run npm i create-react-app myapp then yoou need to install it first. Checkout here: https://www.npmjs.com/package/create-react-app

Related

Vue.js how to import SVG logo

I'm new to vue.js and am used to React. I'm currently trying to import an SVG logo into my header component but I'm not sure how. In react, I would simply do import Logo from './path; and use Logo wherever I needed it within the current component. This is basically what I'm attempting to do right now but I keep getting errors. Could anyone tell me how this could be done in Vue.js?
<template>
<header class="nav">
<img src={Logo} alt="24G Logo">
</header>
</template>
<script>
import Logo from '../assets/76_logo.svg';
export default {
name: 'Header'
}
</script>
<style lang="scss" scoped>
</style>
Here are three options. The best in my opinion is the third:
Simply input src like in any webpage <img src='../path/to/file.svg' ... though that come with some drawbacks (regardless if it's :src='logoPath' where logoPath is variable containing the same. For a short overview see this stack answer, and for more details see this article from css tricks.
Check out svg-vue-loader. Vue won't automatically import svg without a loader.
Just paste it in! (Open the svg file and copy paste it into the template.) The best option in my opinion, especially when prototyping or for smaller projects. Like so:
<template>
<header class="nav">
<svg ....
</header>
</template>
If that would make it too crowded later on, just make a new component, call it say Logo, and paste svg in there and then import MainLogo component into your header.
No need for svg-loaders. Though loaders are a dev dependency, so not like it would cost you anyway; they would just do the same thing you can do manually.
// in MainLogo.vue
<template>
<svg ....
</template>
// in MainHeader.vue
<template>
<header class="nav">
<MainLogo>
</header>
</template>
<script>
import MainLogo from '../path/to/file.vue'
export default {
components: { MainLogo }
}
</script>
Cheers
After searching and searching, and seeing all the answers were old, I went ahead and tried the newish v-html prop.
The result, success!
<div v-html="avatar" style="width: 100%"></div>
The avatar is a full element that I stored in the database.
No loaders, no imports, just using the built in resources of Vue.js
If you leave out the style, then the svg will not show.
Also, loading the full element enables me to attach a ref prop to the element. Enabling me to access the svg through script.
Hope that helps someone!!
I used this with avataaar's random avatar generator and stored the resulting svg to the database (mongo)
Here is another approach that I used:
<template>
<a href="#"
class="log-link-css-class">
<!-- SVG Icon Start-->
<img alt="alt message" class="your-logo-css-class"
src="#/assets/images/logofilename.svg">
<!-- <SVG Icon End /> -->
</a>
</template>
No import required. Vue automatically converts it to the unique URL.
edit your code
<script>
import Logo from '../assets/76_logo.svg';
export default {
name: 'Header',
data(){
Logo: Logo
}
}

How to create external download link in React?

How to create link with preventDefault and on click it must run download file from external url in React
return (
<Link to="/page">
<img src={src} />
<a href="https://external_photo_uri/pic.jpg" download />
</Link>
)
You can create a normal anchor tag with download attribute.
Example
<a href="myurl" download> <img src={src} /></a>
You can use react-download-link
A simple component to download data from a client-side cache (e.g. flux, redux). Design to be used with browserify or webpack.
Install with:
npm install --save react-download-link
Include with:
import DownloadLink from "react-download-link";
Use:
<DownloadLink
filename="myfile.txt"
exportFile={() => "My cached data"}
>
Save to disk
</DownloadLink>
Or with Promises:
<DownloadLink
filename="myfile.txt"
exportFile={() => Promise.resolve("My cached data")}>
Save to disk
</DownloadLink>
The component will default to an anchor tag, but the tagName prop will accept a string of any other HTML tag you prefer, such as 'button'.
I solved this problem. I create parent div on anchor tag and onClick run e.stopPropagation()
Please try with this one.
<!DOCTYPE html>
<html>
<body>
<p>Click on the w3schools logo to download the image:<p>
<a href="/images/myw3schoolsimage.jpg" download>
<img src="/images/myw3schoolsimage.jpg" alt="W3Schools" width="104" height="142">
</a>
<p><b>Note:</b> The download attribute is not supported in Edge version 12, IE, Safari 10 (and earlier), or Opera version 12 (and earlier).</p>
</body>
</html>
Reference: https://www.w3schools.com/tags/att_download.asp

Images not loading after using 'npm run build'

I'm still pretty new to react and I have an application that I'm trying to deploy to the web. I've noticed that when I use 'npm run build' it doesn't populate my chunk.js scripts with a '.' before the location. That's an easy fix, I just add the '.' manually and move on with my life. But now it's doing the same thing with images, and these are rendered dynamically so it's not an easy manual fix once the build is run. I've got code below.
For example: <script src="/static/js/2.4724d625.chunk.js"></script>instead of <script src="./static/js/2.4724d625.chunk.js"></script>
I tried manually entering the period like so:
const Certificate = ({
firstName,
lastName,
completionDate,
userToken,
division
}) => (
<div id="box" className="container-fluid text-center">
<div id="nice-border">
<div className="row">
<div className="col-12">
<div id="sov-logo">
// this is where i manually put a . before the logo directory
<img src={`.${Logo}`} alt="Logo" />
</div>
</div>
</div>
but that didn't help at all. It seems that the build just ignores that. So how can I get these images to work properly once it's deployed on a server?
This is how the image looks when I inspect it in the browser: <img src="/static/media/capitolREDback.dad6f9b2.jpg" alt="Logo"> and as soon as I add a dot like so: <img src="./static/media/capitolREDback.dad6f9b2.jpg" alt="Logo"> it loads the image just fine. How do I fix this?
In your package.json, set "homepage" to "."
Then you can build and deploy correctly.

Load Reactjs Bootstrap styling

I'm very new to reactjs. There is a third-party library called react-bootstrap that I want to use in my project. I installed it via yarn and when I want to use it's components, nothing happens! For example:
import { Button } from 'react-bootstrap'
<Button variant="success">Sign In</Button>
has no effect on a button. In elements I can see that the class assigned:
<button type="button" class="btn btn-success">Sign In</button>
but there isn't any effect in button!! What should I do?
If you need any clarification, simply ask for it. Thank you!
You need to add bootstrap.css, either by adding link in index.html
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
/>
or by importing in index.js file after installing bootstrap using yarn / npm.
import 'bootstrap/dist/css/bootstrap.min.css';
Demo

Material Icons just rendering text, React Materialize

Currently working on a group project for class. It's using React and MaterializeCss on the front.
I'm trying to add icons, for example, on the login screen I have a little person icon next to the username input field, but it just displays "ACCOUNT_USER" or on the send button, I want to show an icon called send but it just displays "SEND". Here's a code example.
<div className="row">
<div className="input-field col s6 offset-m3">
<i className="material-icons prefix">account_circle</i>
<input id="username" type="text" className="validate" />
<label htmlFor="username">Username</label>
</div>
Before anyone tells me to use include the CDN line from https://materializecss.com/getting-started.html , I already have.
<!--Import Google Icon Font-->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
Does anyone have any idea what could be the problem then? The program currently runs locally hosted, and I read that could be a problem with materialize, and the only apparent solution I found so far is to download the icons package from google, but that would be silly to download 91k icon files for a program no? Hopefully there's a better solution
How to use materialize-css with React with icons and other js features
yarn add materialize-css#next --save or npm install materialize-css#next --save
yarn add install material-icons or npm install material-icons
3.Kindly import the following like this ; snap
import 'materialize-css/dist/css/materialize.min.css';
import 'materialize-css/dist/js/materialize';
import 'material-icons/iconfont/material-icons.css'

Resources