简体   繁体   English

样式在 nextjs 中刷新

[英]styling changing on refresh in nextjs

I'm having this weird issue where my styling is not sticking.我遇到了这个奇怪的问题,我的样式没有坚持。 I have a NavBar set to be 20vh in height.我将 NavBar 的高度设置为 20vh。 I also have an image set to be 100% in width.我也有一个图像设置为 100% 的宽度。 However, when I refresh the page, the NavBar height shrinks and the image width increases significantly.但是,当我刷新页面时,NavBar 高度会缩小,图像宽度会显着增加。 I'm not sure what is causing this problem.我不确定是什么导致了这个问题。 I have pasted the relevant code below but you can also find the repo for the app at this link (dev branch).我在下面粘贴了相关代码,但您也可以在此 链接(开发分支)上找到该应用程序的存储库。

const useStyles = makeStyles((theme) => ({
  navBar: {
    height: "20vh",
    width: "100%",
  },
}));

const NavBar = () => {
  const classes = useStyles();
  return <div className={classes.navBar}>NavBar</div>;
};
const useStyles = makeStyles((theme) => ({
  introImg: {
    width: "100%",
    height: "auto",
  },
}));

const Intro = () => {
  const classes = useStyles();
  return <img src={marco4sup} className={classes.introImg} />;
};

As you can see, the NavBar is definitely not 20vh and the image is expanding beyond 100% of the page width.如您所见,NavBar 绝对不是 20vh,并且图像正在扩展超过 100% 的页面宽度。

在此处输入图像描述

Alert: I will give a pull request on the repo on github so you can implement the code.警告:我将在 github 上的 repo 上发出拉取请求,以便您可以实现代码。 Fast Refresh is a Next.js feature that gives you instantaneous feedback on edits made to your React components.快速刷新是 Next.js 功能,它可以为您提供对 React 组件所做编辑的即时反馈。 Fast Refresh is enabled by default in all Next.js applications on 9.4 or newer.在 9.4 或更高版本的所有 Next.js 应用程序中,默认启用快速刷新。 With Next.js Fast Refresh enabled, most edits should be visible within a second, without losing component state.启用 Next.js 快速刷新后,大多数编辑应该在一秒钟内可见,而不会丢失组件 state。

How It Works If you edit a file that only exports React component(s), Fast Refresh will update the code only for that file, and re-render your component.它是如何工作的 如果你编辑一个只导出 React 组件的文件,Fast Refresh 将只更新那个文件的代码,并重新渲染你的组件。 You can edit anything in that file, including styles, rendering logic, event handlers, or effects.您可以编辑该文件中的任何内容,包括 styles、渲染逻辑、事件处理程序或效果。 If you edit a file with exports that aren't React components, Fast Refresh will re-run both that file, and the other files importing it.如果你编辑一个包含非 React 组件的导出文件,Fast Refresh 将重新运行该文件以及导入它的其他文件。 So if both Button.js and Modal.js import theme.js, editing theme.js will update both components.因此,如果 Button.js 和 Modal.js 都导入了 theme.js,编辑 theme.js 将更新这两个组件。 Finally, if you edit a file that's imported by files outside of the React tree, Fast Refresh will fall back to doing a full reload.最后,如果您编辑由 React 树之外的文件导入的文件,Fast Refresh 将回退到完全重新加载。 You might have a file which renders a React component but also exports a value that is imported by a non-React component.你可能有一个文件,它渲染了一个 React 组件,但也导出了一个由非 React 组件导入的值。 For example, maybe your component also exports a constant, and a non-React utility file imports it.例如,也许你的组件也导出了一个常量,而一个非 React 实用程序文件导入了它。 In that case, consider migrating the constant to a separate file and importing it into both files.在这种情况下,请考虑将常量迁移到单独的文件并将其导入到这两个文件中。 This will re-enable Fast Refresh to work.这将重新启用快速刷新工作。 Other cases can usually be solved in a similar way.其他情况通常可以用类似的方式解决。 Arrow functions aren't supported.不支持箭头函数。 Name your functional component.命名您的功能组件。

export default function MyPage () {...

Without export defualt function MyPage() {... it won't use fast refresh therefore your code won't work, to implement it into your Code do the following(for code block1):如果没有 export defualt function MyPage() {... 它不会使用快速刷新,因此您的代码将无法工作,要将其实现到您的代码中,请执行以下操作(对于代码块 1):

export default function UseStyles () {
  navBar: {
    height: "20vh",
    width: "100%",
  },
}));

const NavBar = () => {
  const classes = useStyles();
  return <div className={classes.navBar}>NavBar</div>;
};

Sorry if there are any grammatical errors my english isn't great.抱歉,如果有任何语法错误,我的英语不是很好。 And also if you want your navbar to be sticky set the position to sticky like the following:此外,如果您希望导航栏具有粘性,请将 position 设置为粘性,如下所示:

position: sticky;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM