Can't connect Google Analitycs with React.js - reactjs

how are you doing? I badly cannot connect GA with React, im very stucked and all the videos that i saw the ID connector its start with UA, please help me
import './App.css';
// import Header from "../Header/header"
import Header2 from "../Header2/header"
import Footer from "../Footer/footer"
import Switch from "../Switch/switch"
import {BrowserRouter as Router} from "react-router-dom";
import React, {useEffect} from "react"
import ReactGa from "react-ga";
/**** Icons ****/
import { library } from '#fortawesome/fontawesome-svg-core'
import { fab, faFacebookSquare, faGoogle, faInstagramSquare, faWhatsappSquare} from '#fortawesome/free-brands-svg-icons'
import { fas, faBars, faIdCard, faGlobeAmericas, faCalendarAlt, faArrowUp} from '#fortawesome/free-solid-svg-icons'
library.add(fas, fab, faBars, faFacebookSquare, faInstagramSquare, faWhatsappSquare, faGoogle, faIdCard, faGlobeAmericas, faCalendarAlt, faArrowUp)
function App() {
useEffect(() => {
ReactGa.initialize('G-YZ9S63BMYY')
ReactGa.pageview(window.location.pathname)
}, [])
useEffect(() => {
console.log("hola", window.location.pathname)
})
return (
<Router>
<Header2 />
<React.StrictMode>
<Switch />
</React.StrictMode>
<Footer />
</Router>
);
}
export default App;
From now on thanks for all folks :)

You have to use React for GA4: https://www.npmjs.com/package/react-ga4

Related

Why doesn't my fontawesome icon render in my Next.js project?

I have a Next.js project where I am using fontawesome icons.
However, the icon does not show up on the web page. I even checked under inspect element and in the parent div and the icon element is not there.
Below is my _app.js file:
import '../styles/globals.css'
import { config } from '#fortawesome/fontawesome-svg-core'
import '#fortawesome/fontawesome-svg-core/styles.css'
config.autoAddCss = false
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
export default MyApp
And below is my index.js file in the pages directory:
import React, { Fragment } from "react"
import Head from 'next/head'
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import '../node_modules/#fortawesome/fontawesome-svg-core/styles.css'
export default function Home() {
return (
<Fragment>
<FontAwesomeIcon icon="fa-brands fa-facebook-f" />
</Fragment>
)
}
Why isn't my icon rendering and how do I fix it?
Thanks in advance.
You still need to import icons into your project. That's why there is nothing showing. This is what your index.js should look like if using the free brands svg icons:
import React, { Fragment } from 'react'
import Head from 'next/head'
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faFacebookF } from '#fortawesome/free-brands-svg-icons'
export default function Home() {
return (
<Fragment>
<FontAwesomeIcon icon={faFacebookF} />
</Fragment>
)
}

i18next React - Changing language only works after browser refresh

On my React page, changing the language only works after browser refresh. Have tried all possible suggestions from previous posts and nothing. It does work as expected only for strings on the App.js component. Any help appreciated.
here is my i18n.js file
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
const resources = {
'en': {translation: require('./translations/en.json')},
'th': {translation: require('./translations/th.json')},
'pt': {translation: require('./translations/pt.json')},
}
i18n
// detect user language
// learn more: https://github.com/i18next/i18next-browser-languageDetector
.use(LanguageDetector)
// pass the i18n instance to react-i18next.
.use(initReactI18next)
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init({
debug: true,
fallbackLng: 'en',
// lng: 'th',
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
},
resources
});
export default i18n;
My change language function is this:
handleLanguageChange = (item) => {
i18n.changeLanguage(item.key, () => console.log('lanh1', i18next.language))
// this.forceUpdate()
}
Importing on App.js file:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import rootReducer from './store/reducers/rootReducer';
import thunk from 'redux-thunk';
import { applyMiddleware, createStore } from 'redux';
import { Provider } from 'react-redux';
import './i18n';
const store = createStore(rootReducer, applyMiddleware(thunk));
ReactDOM.render(
<Provider store={store}>
<React.StrictMode>
<App />
</React.StrictMode>
</Provider>,
document.getElementById('root')
);
reportWebVitals();
and here is how i render translations:
import React, { Component } from 'react';
import { Navigate} from "react-router-dom";
import { Button, Container, Grid } from '#mui/material';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { setCurRoute } from '../../store/actions/authAction';
import Background from '../../img/header-img.jpg';
import i18next from 'i18next';
// import i18n from '../../i18n';
class LandingPage extends Component {
render() {
return (
<div
style={{
backgroundImage: `url(${Background})`,
backgroundPosition: 'center',
backgroundSize: 'cover',
backgroundRepeat: 'no-repeat'
}}
>
<h1 style={{textAlign: 'center'}}>{i18next.t('headline_one_one')}<br />{i18next.t('headline_one_two')}</h1>
<h4 style={{textAlign: 'center', lineHeight: 2, border: '2'}}>
{i18next.t('headline_two')}
</h4>
</div>
);
}
}
export default connect(mapStateToProps, mapDispatchToProps)(LandingPage)
In react, try to use the useTranslation hook or the withTranslation HOC. That will detect the language change.
Try this if it's a class component:
componentDidUpdate(prevProps, prevState) {
if (this.props.t !== prevProps.t) {
console.log(this.props.i18n.language);
}
}
As NextJs pre-rendering is Static Generation or Server-side Rendering of the html of the pages, we cannot use i18n.changeLanguage like in a "normal" React app and we have to use the next router like this :
import {useRouter} from "next/router";
const router = useRouter();
function changeLang(locale:string):void {
router.push({
route: router.route,
query: router.query
}, router.asPath, { locale });
}
changeLang("en");

Micro-frontend cannot find module but working in React host

I did a simple counter micro frontend for practice purposes, its working as expected but im getting an error message from visual code "Cannot find module 'remote/counterWrapper'...
Print:
My remote code:
import React, {useRef, useEffect} from "react";
import ReactDOM from "react-dom";
import counterWrapper from "remote/counterWrapper";
import "./index.css";
const App = () => {
const divRef = useRef(null)
useEffect(()=> {counterWrapper(divRef.current);}, [])
return(
<div>
<div>Name: react-host</div>
<div ref={divRef}></div>
</div>
)
};
ReactDOM.render(<App />, document.getElementById("app"));
My host code:
import { render } from "solid-js/web";
import Counter from "./Counter"
import "./index.css";
export default (el) => {
render(Counter, el);
}

I am trying to setup React storybook with an ejected version of create-react-app

I am trying to setup React storybook with an ejected version of create-react-app.
// config.js
import { configure } from '#storybook/react';
import i18n from 'i18n';
import { I18nextProvider } from 'react-i18next';
const Root = story => (
<I18nextProvider i18n={i18n}>
<App {...rest} />
</I18nextProvider>
);
function loadStories() {
require('../src/components/Home/Slip/Slip.story.js');
// You can require as many stories as you need.
}
configure(loadStories, module);
Following is the story :
import _ from 'lodash';
import React from 'react';
import { storiesOf } from '#storybook/react';
import Slip from './Slip.component';
const data = {...}
storiesOf('Footer', module).add('with text', () => (
<Slip data={data} />
));
I am getting
enter image description here
Can someone help me out here ?

Material UI - outlined button - "Unexpected use of 'self' no-restricted-globals"

Trying to use an outlined button from material UI : https://github.com/mui-org/material-ui/blob/master/docs/src/pages/demos/buttons/OutlinedButtons.js
But get the error - "Unexpected use of 'self' no-restricted-globals"
And I don't have permissions to change it in the lodash.throttle/index.js file.
Is there any workaround for this that anyone knows of?? (It's literally just the sample code btw)
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '../node_modules/#material-ui/core/styles';
import Button from '../node_modules/#material-ui/core/Button';
const styles = theme => ({
button: {
margin: theme.spacing.unit,
},
input: {
display: 'none',
},
});
function OutlinedButtons(props) {
const {classes} = props;
return (
<div>
<Button variant="outlined" className={classes.button}>
Default
</Button>
</div>
);
}
OutlinedButtons.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(OutlinedButtons);
After a request. App.js:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import PropTypes from 'prop-types';
import { withStyles } from '#material-ui/core/styles';
import Button from '#material-ui/core/Button';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import UserGroup from './components/user_group';
import ButtonTest from './components/Button';
import OutlinedButtons from './components/floatingButton'
class App extends Component {
render() {
return (
<MuiThemeProvider>
<div className="App">
<header className="App-header">
{ OutlinedButtons }
</header>
</div>
</MuiThemeProvider>
);
}
}
export default App;
And index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

Resources