I'm trying to figure out why the loadeddata/loadedmetadata event is not firing in my application. Actually, sometimes it does fire but it's inconsistent. I suspect there is some kind of race condition going on here but after a lot of trial and error and quite a lot of frustration, I'm out of ideas.
So, the idea is simple. I have an <audio> element and I want to run some logic when it is loaded.
This seems to work when I try it in a non-Nextjs React application. Example here
However, when I run the same thing in my Next.js React application locally I observe the aforementioned behaviour, so I suspect that this could be nextjs specific?
This can be minimally reproduced by:
Running npx create-next-app nextjs-blog --use-npm --example "https://github.com/vercel/next-learn-starter/tree/master/learn-starter"
Replacing the existing index.js with:
export default function Home() {
const handleMetadata = () => {
alert("hi")
}
return (
<div className="container">
<main>
<audio
id="audio"
onLoadedData={handleMetadata}
onLoadedMetadata={handleMetadata}
src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3"
/>
</main>
</div>
)
}
Repeatedly refreshing the browser window
you should load the compoent which use audio with no ssr.like this:
import dynamic from 'next/dynamic'
const AudioPlayer = dynamic(import('../components/Home'), {
ssr: false
})
This isn't a direct answer but I wanted to share that #YAN7 answer above solved a similar issue for me. I was developing a component in Storybook that was using GSAP scroll trigger to scrub play a video. It worked perfectly in Storybook, but when I went to import it and use it in the UI/Next Js project, it wouldn't work as expected. Occasionally I managed to get the animation effect to work, but most of the time I couldn't. After a lot of console logging, I realised the only significant difference between the two examples was one was in Next and the other wasn't. Which eventually lead me here. I dynamically imported my component into Next as in YAN7's example and it worked perfectly. Many thanks YAN7! I hope this comment helps other GSAP + Next devs in the future
Related
I have a React/Next.js component Onboarding where I'm using withPageAuthRequired from #auth0/nextjs-auth0 that looks like
export default function Onboarding() {
return (
<Layout>
<h2>Onboarding</h2>
</Layout>
)
}
export const getServerSideProps = withPageAuthRequired()
It works perfectly in the app itself, but Storybook fails to build due to errors such as
node_modules/#auth0/nextjs-auth0/dist/node_modules doesn't exist or is not a directory.
I tried a few Storybook add-ons, but they didn't work at all to make withPageAuthRequired work with Storybook. The only thing that worked was when I made a copy of the Onboarding file where I didn't use withPageAuthRequired and just imported that component into Storybook. However, this is not an optimal solution since the two files are basically copies of each other, and I don't want to do this for every page of my app.
Does anybody know what to do here or if there's a workaround?
I am continuously running into this error while trying to learn react native. The app will run fine and then I make some additions and it breaks and shows this error:
The code that is producing it is:
import React from 'react';
import {StyleSheet, View, Text} from 'react-native';
const App = () => {
return (
<View>
<Text>Hello</Text>
</View>
);
};
const styles = StyleSheet.create({});
export default App;
Any information would be awesome, I just want a consistent dev environment to be able to work in. I have looked through similar posts however they all seem to already have functioning projects when receiving this error.
The fix is that it was a problem with the react-native debugger that apparently just happens sometimes, disable the debugger with ctr+m on the android device and debug using node command line or third party debugger
Running below command fixed the issue for me.
cd android
./gradlew clean
I have faced similar issue. Apparently, my react native debugger is trying to reference a module from previous react native folder.
(That time, I created 2 same react native folder, one as a backup. In case I messed up with the coding)
In order to fix this, I have to clear the react native cache. I found my answer in this post.
I had a certain bug that I was unsure how to debug. The bug is that styling that I use for a create-react-app (Typescript) app through material-ui and regular CSS, shows up just fine in development but it shows differently for certain CSS properties when I deploy the app online (I've only tried Heroku and Vercel). The app doesn't show any errors or warnings in the console when I'm developing. I have no idea why it's happening and I've tried the following so far
Read through my code many times being extra careful at every step.
Open the website (development and production) in incognito mode on Chrome 85.0.4183.83 (Linux, 64 bit)
Look through the documentation for material-ui (material-ui.com) and the React docs for any lead on this
Search google for prior such issues
So far, nothing has turned up. Could someone please guide me in the right direction?
The build pack I use on Heroku after deployment is the well known one by mars and available at https://buildpack-registry.s3.amazonaws.com/buildpacks/mars/create-react-app.tgz .
For convenience, I have included my package.json here on pastebin.
Thank you so much for helping out.
============= EDIT =============
I've been experimenting with the code and it seems like the problem lies with how material-ui is compiled during build time. The problems which don't show up in development, show up in production.
For example, the biggest thing I noticed is if you mix custom style prop flags given by the material-ui components and add your own styling with className or style, during build time these latter styles are overridden partially if not completely.
So if I do
import React,
{ useRef } from "react";
import {
Button,
makeStyle,
Theme
} from "#material-ui/core";
/*
Start of functional component using memo
.
.
.
*/
const useClasses = useRef(makeStyle((theme : Theme) => { return {
buttonStyles:
{
backgroundColor: theme.palette.text.hint
}
}}));
const styles = useClasses.current();
<Button
color={"secondary"}
className={styles.buttonStyles}>
button text
</Button>
/*
.
.
.
*/
in development, the button will have the backgroundColor I give it in makeStyles, but in production that property will switch to secondary as given in the color prop which defaults to the value theme.palette.secondary.main.
And sometimes, just even using a component from material-ui without any extra config causes a problem.
I am also using code splitting with lazy and Suspense if that matters.
StackMatch can you please link your github/gitlab repo? I think I might know what the problem is but can't be certain without seeing how your dev environment is configured.
have very simple sample app which build Create React App + Styled-Components to prove this issue. But I have real big application which I am facing this issue which I am going to explain it below.
I would like to pre-render this app with Rendertron for SEO/GoogleBots and etc. But the problem is when I build PRODUCTION version of React App which use Styled-Components . all the style will be missing on static version which Rendertron produced, but from other side if I try the same workflow with dev-server of app , everything looks fine .
So far I know there is different on PROD version and DEV version of my application when I render it with Rendertron . But I am not sure what cause this issue and how I can fix this issue .
I am looking for solution or idea which can help me to solve this issue .
Here is my sample code which I peppered for test .
https://github.com/AJ-7885/test-styled-component-with-rendertron
Here is screen shot from different version of Rendered version by Rendertron base on PROD or DEV version of the same application .
enter image description here
After a lot of searching around, I finally found out the reason. The Styled Components library uses something called the "Speedy mode" to inject styles on production. This makes the styles bypass the DOM` and be injected directly inside the CSSOM, thus, appearing in the inspector, but totally invisible on the DOM.
Fortunately, Styled Components 4.1.0 came with a fix for this issue! Now you can set a global variable called SC_DISABLE_SPEEDY to true in order to disable the Speedy mode and get the styles to appear on Production as well.
Reference: https://www.styled-components.com/releases#v4.1.0
But the only part I am not sure , how to set disable this Speedy Mode in Create-React-App without Ejecting , Dose any body has any idea ?
You need to render your styles on the server side and inject those styles in your pre-rendered react app. Styled-components explains how to do that here: https://www.styled-components.com/docs/advanced#server-side-rendering
Also, I'd recommend using react-snap for pre-rendering since that is recommended by the Create React App docs. react-snap seems to be more of a React-specific solution that may be easier to implement, especially with styled-components.
Just experimenting with antd's React based UI kit, however it seems that some of the input components not rendering as they should, ex. as a dropdown.
Please see below a random component and how it's being rendered (I added 2 components within mine, TimePicker and DatePicker to demonstrate this)
Looking through the docs I understood that these input or form components must be wrapped within < Form > and < Form.Item >, I tried with and without these.
I also tried both with and without providing default props, still nothing, the documentation doesn't mention these as mandatory ones.
render() {
return (
<div>
<Form>
<Form.Item>
<TimePicker/>
<DatePicker/>
</Form.Item>
</Form>
</div>
);
}
actual: [actual render][1]
expected: https://ant.design/components/time-picker
Thanks guys!
update: I created a new react app on my work mac using npx create-react-app (yesterday I used my personal windows) and had npm i antd. I made sure I have nothing in the new app which could cause issue, only the boilerplate, then I added a component exactly how it is in the ant design documentation.
I am still experiencing the same issue. Sure it's missing something fundamental within ant-design but it seems it's not in the documentation. Anyone who ever used the ant-design components with react, have you done anything specific apart from installing the package and importing the components before using?
It looks like it disregards the css.
In case if anyone else is experiencing this, found the solution:
Antd's css was not applied because I forgot to import it to my App.css:
#import '~antd/dist/antd.css';
Simple mistake to make.
Solution was found in the below link:
https://github.com/ant-design/babel-plugin-import/issues/89
mthrsj also asked about this, I overlooked it when I checked.