React Hook "useState" cannot be called at the top level - react-hooks

I am a beginner in react. I'm trying to send values of my database from one file to another using react-hooks, and getting the following error can anyone help me with this?
Line 5:41: React Hook "useState" cannot be called at the top level. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks
Code:
import React, {useState, useEffect} from "react";
import Axios from "axios";
const [collegeList, setcollegeList]=useState([]);
useEffect(()=>{
Axios.get("http://localhost:3001/api/get").then((response)=>{
setcollegeList(response.data);
});
}, []);
CollegeList = collegeList;
export default CollegeList;

You must use react hooks in react components. There're two ways i can recommend to you to fix this.
first) Doing it in your component
import React, {useState, useEffect} from "react";
import Axios from "axios";
function CollegeListComponent() {
const [collegeList, setcollegeList]=useState([]);
useEffect(()=>{
Axios.get("http://localhost:3001/api/get").then((response)=>{
setcollegeList(response.data);
});
}, []);
return "YOUR VIEW ELEMENTS"
second) Use it as react hook
import React, {useState, useEffect} from "react";
import Axios from "axios";
// making a custom react hook like this
function useCollegeList() {
const [collegeList, setcollegeList]=useState([]);
useEffect(()=>{
Axios.get("http://localhost:3001/api/get").then((response)=>{
setcollegeList(response.data);
});
}, []);
return { collegeList }
and then you can import useCollegeList in your component to use it

Related

Laravel 9 Vue 3 Inertia Project

I'm building an SPA with Laravel and Vue.js with vite, and I have another project with Laravel Breeze and Inertia js for the CMS. How can I merge these 2 projects into one ? So I can have an SPA project with CMS on it like an admin page that u can set through the routes.
Is it possible ?
This is the app.js in my CMS project
import '../css/app.css';
import { createApp, h } from 'vue';
import { createInertiaApp } from '#inertiajs/vue3';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { ZiggyVue } from '../../vendor/tightenco/ziggy/dist/vue.m';
const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';
createInertiaApp({
title: (title) => `${title} - ${appName}`,
resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
setup({ el, App, props, plugin }) {
return createApp({ render: () => h(App, props) })
.use(plugin)
.use(ZiggyVue, Ziggy)
.mount(el);
},
progress: {
color: '#4B5563',
},
});
This is the app.js in my SPA project
import '../css/navbar.css';
import '../css/home.css';
import '../css/kenapa.css';
import '../css/ceritakami.css';
import '../css/karir.css';
import '../css/lowongan.css';
import '../css/media.css';
import '../css/gabung.css';
import '../css/mitra.css';
import '../css/download.css';
import '../css/footer.css';
import { createApp } from 'vue';
import App from './layouts/app.vue';
createApp(App).mount('#app');
I tried to copy the pages into the CMS and vice versa, but It just keep giving me errors.
I'm a beginner and I just don't get it using inertia, because in my SPA project, I'm using App.vue as a layout to call other pages like this :
<NavbarVue/>
<Home/>
<Kenapa/>
<Cerita/>
<Karir/>
<Lowongan/>
<Media/>
<Gabung/>
<Mitra/>
<Download/>
<Footer/>
</template>
<script setup lang="ts">
import NavbarVue from '../components/Navbar.vue';
import Home from '../pages/Home.vue';
import Media from '../pages/Media.vue';
import Kenapa from '../pages/Kenapa.vue';
import Cerita from '../pages/Cerita.vue';
import Lowongan from '../pages/Lowongan.vue';
import Karir from '../pages/Karir.vue';
import Gabung from '../pages/Gabung.vue';
import Mitra from '../pages/Mitra.vue';
import Download from '../pages/Download.vue';
import Footer from '../components/Footer.vue';
</script>
But in Inertia I can't do that, Can someone enlighten me ?
Migration
Make a new Laravel 9 project and follow the instructions for installing Breeze w/ Inertia nad Vue.
https://laravel.com/docs/9.x/starter-kits#breeze-and-inertia
You will then move the *.vue files over to the new project and parse them appropriately.
For instance, you may get data via Axios calls in your Spa. With Inertia, you pass data directly from the controllers.
Furthermore, you will have to import your controllers and all related PHP files into the new project and parse them to align with the Inertia design structure.
Testing
If you haven't already, I highly suggest you get into testing with PHPUnit. Properly tested code can make migrating files from one repository to another less nerve-wracking.
https://laravel.com/docs/9.x/testing
Conclusion
Overall, this would be a massive project that could possibly take months. But if you intend to combine two completely different projects into one, that complexity will require it, especially if neither is tested.

how to import vue component into laravevel app.js file?

I am trying to import vue into laravel appjs file but am seeing no result.
My component is called Dash.vue and its located in 'resources\js\components'
The code below is what i have tried
import Dash from 'resources\js\components';
new Vue({
components: {
"Dash": YourComponent
});
try with:
import Dash from './resources/js/components/Dash';
new Vue({
components: {
Dash
});

Vuetify in Storybook gets error: undefined is not an object (evaluating 'this.$vuetify.theme.dark')

I'm trying to use Vuetify in Storybook with costume setup. The .Storybook/config.js looks like:
import { configure, addDecorator } from '#storybook/vue';
import 'vuetify/dist/vuetify.css';
import Vue from 'vue';
import Vuetify from 'vuetify';
Vue.use(Vuetify, {
theme: {
dark: {
primaryTypColor: '#232B2B',
productCardTitle: '#7E7F80'
},
light: {
primaryTypColor: '#232B2B',
productCardTitle: '#7E7F80'
},
},
});
addDecorator(() => ({
template: '<v-app><story/></v-app>',
}));
configure(require.context('../stories', true, /\.stories\.js$/), module);
I'm getting the error: undefined is not an object (evaluating 'this.$vuetify.theme.dark') why is that?
It is because you haven't injected the global vuetify object in the Vue.js context. You can do so with a decorator.
In your example it looks like you only need to add the Vuetify that you imported to the addDecorator function as a parameter.
A complete example for storybook versions below 6, here is how:
import Vue from 'vue';
import vuetify from '#plugins/vuetify'
Vue.use(vuetify)
addDecorator(() => ({
template: '<v-app><story/></v-app>',
vuetify,
}));
For storybook version 6:
import vuetify from '#plugins/vuetify'
import Vue from 'vue'
Vue.use(vuetify)
export const decorators = [
() => ({
template: '<v-app><story/></v-app>',
vuetify
}),
]

Using prerender-spa-plugin with Laravel Vue.js in the resources directory

I have installed the prerender-spa-plugin via npm in my Laravel app that has the Vue.js app in the resources directory. How do I call the prerender-spa-plugin
in it? I have seen quite a bit examples of a standalone app but nothing that will support it in the resources directory.
To use plugin in a component only: known as local registration
<script>
import 'plugin_name' from 'plugin_directory' //located at /node_modules
//to use it in component call it like shown below
export default{
...
}
</script>
Then use it according to its application
To use it in multiple components/views. Known as global registration
In app.js import component and register it
import Plugin from 'plugin_directory'
Vue.use(Plugin)
For example. to use axios, we import it and register it globally in app.js like this
import axios from 'axios'
Vue.use(axios)
//that's it
This is how we can register multiple plugins globally in Vue in app.js
require('./bootstrap');
import Vue from 'vue'
import axios from 'axios';
import VueAxios from 'vue-axios';
Vue.use(VueAxios,axios);
import VueRouter from 'vue-router';
Vue.use(VueRouter);
Vue.config.productionTip =false;
import VeeValidate from 'vee-validate';
Vue.use(VeeValidate);
import Notifications from 'vue-notification'
Vue.use(Notifications)
import VueHolder from 'vue-holderjs';
Vue.use(VueHolder)
import App from './layouts/App'
import router from './router'
Vue.component('example-component', require('./components/ExampleComponent.vue'));
Vue.component('autocomplete', require('./components/AutoComplete.vue'));
Vue.component('topnav', require('./components/Nav.vue'));
Vue.component('imageupload', require('./components/ImageUpload.vue'));
const app = new Vue({
el: '#app',
router,
template:'<App/>',
components:{ App }
});

Vue.js how to use npm plugins

I have tried multiple different plugins with my Vue.js cli webpack projects and I am getting the same problem with all of them, namely that once I have included a plugin, I am unable to use it.
As an example. I have tried to use the plugin at this link. https://www.npmjs.com/package/vue-sessionstorage
Here is my main.js file
import Vue from 'vue'
import router from './router'
import VueMaterial from 'vue-material'
import { store } from './store'
import 'vue-material/dist/vue-material.css'
import 'vue-style-loader'
import VueSessionStorage from 'vue-sessionstorage'
import App from './App'
Vue.use(VueMaterial, VueSessionStorage);
Vue.config.productionTip = false;
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: { App },
});
I have run npm install --save vue-sessionstorage
As you can see, I have also imported VueSessionStorage
However, when I try to use it like this
methods: {
login: function () {
console.log("Login clicked");
this.$session.set('username','simon')
console.log(this.$session.get('username'));
},
register: function () {
console.log(this.$session.get('username'));
}
}
I get the error
vue.esm.js?65d7:563 TypeError: Cannot read property 'set' of undefined
at VueComponent.login (Login.vue?2ddf:35)
at boundFn (vue.esm.js?65d7:179)
at Proxy.invoker (vue.esm.js?65d7:1821)
at Proxy.Vue.$emit (vue.esm.js?65d7:2331)
at click (mdButton.vue?3bf8:38)
at HTMLButtonElement.invoker (vue.esm.js?65d7:1821)
handleError # vue.esm.js?65d7:563
Vue.$emit # vue.esm.js?65d7:2333
click # mdButton.vue?3bf8:38
invoker # vue.esm.js?65d7:1821
Vue.use does not permit multiple plugins to be initialized at once like that. Initialize them as two separate calls:
Vue.use(VueMaterial);
Vue.use(VueSessionStorage);

Resources