[英]Unable to get dynamic component in Next.js to skip server side rendering and show only on the client
I am trying to dynamically render a custom react component containing react-owl-carousel in a next js application.我正在尝试在下一个 js 应用程序中动态呈现包含react-owl-carousel的自定义反应组件。
However, due to the nature of the react-owl-carousel it cannot be server rendered.然而,由于 react-owl-carousel 的性质,它不能被服务器渲染。 So i decided to skip server side rendering for the dynamic import by setting
ssr
to false like so:所以我决定通过将
ssr
设置为 false 来跳过动态导入的服务器端渲染,如下所示:
const Testimonials = dynamic(
() => import('../components/home/Testimonials'),
{
ssr: false,
loading: () => <p>...loafing</p>
}
)
The complete Home component looks like this:完整的 Home 组件如下所示:
import React from 'react';
const Testimonials = dynamic(
() => import('../components/home/Testimonials'),
{
ssr: false,
loading: () => <p>...loafing</p>
}
)
export default class Home extends Component {
render () {
return (
<div>
<Testimonials />
</div>
);
}
The Testimonial component looks like so: Testimonial 组件如下所示:
import React from 'react';
import OwlCarousel from 'react-owl-carousel';
import 'owl.carousel/dist/assets/owl.carousel.css';
import 'owl.carousel/dist/assets/owl.theme.default.css';
const Testimonials = () => {
return (
<div>
<p>skjdjks djks dk sjdk</p>
<OwlCarousel
className="owl-theme"
loop
margin={10}
nav
>
<div class="item"><h4>1</h4></div>
<div class="item"><h4>2</h4></div>
<div class="item"><h4>3</h4></div>
<div class="item"><h4>4</h4></div>
<div class="item"><h4>5</h4></div>
<div class="item"><h4>6</h4></div>
<div class="item"><h4>7</h4></div>
<div class="item"><h4>8</h4></div>
<div class="item"><h4>9</h4></div>
<div class="item"><h4>10</h4></div>
<div class="item"><h4>11</h4></div>
<div class="item"><h4>12</h4></div>
</OwlCarousel>
</div>
);
}
export default Testimonials;
However, i have been battling with the dynamic component to load on the client side for about 4 hours now.但是,我一直在与动态组件作斗争以在客户端加载大约 4 个小时。 On the client side it shows only the loading indicator.
在客户端,它只显示加载指示器。 if i remove the
ssr
option it tries to server render the component and throws an error.如果我删除
ssr
选项,它会尝试服务器呈现组件并引发错误。 Please find attached a screenshot showing what i am currently seeing on the client side.请找到附件截图,显示我目前在客户端看到的内容。
UPDATE: if i comment out everything in the 'Testimonials' component that has to do with OWL Carousel, the component shows the loading indicator and is rendered in the client side.更新:如果我注释掉“推荐”组件中与 OWL Carousel 相关的所有内容,该组件将显示加载指示器并在客户端呈现。 So i am guesing the issue is with Owl carousel.
所以我猜测问题出在 Owl carousel 上。 not sure what it is tho yet.
还不确定它是什么。
Eventually, after much debugging and trying different things i was able to get it to work by following these steps:最终,经过多次调试和尝试不同的事情后,我能够按照以下步骤使其工作:
1) Install jquery npm install jquery --save
1) 安装 jquery
npm install jquery --save
2) Update the next.config.js file to include jquery via the webpack's provide plugin like so: 2) 通过 webpack 的提供插件更新 next.config.js 文件以包含 jquery,如下所示:
const withCSS = require('@zeit/next-css')
const withLess = require('@zeit/next-less')
const webpack = require('webpack');
const withFonts = require('next-fonts');
module.exports = withFonts(withLess(withCSS(
{
webpack: function (config) {
config.module.rules.push({
test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000,
name: '[name].[ext]'
}
}
})
config.plugins.push(new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}))
return config
}
}
)));
In my case i am also using these next js module: @zeit/next-css
@zeit/next-less
, hence my configuration looks like so.就我而言,我还使用了下一个 js 模块:
@zeit/next-css
@zeit/next-less
,因此我的配置看起来像这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.