我一直在使用 nextjs、react 和 redux,但我被卡住了。
我想 ssr 我的条件组件。我的代码有 3 个内容组件,如果用户根据上述条件单击导航栏组件中的链接,它们将呈现。该代码的工作方式与我想要的完全一样,但是当我在 Chrome 中查看源代码时,没有任何<Content />
组件被渲染。这是我的代码:
import React, { Component } from 'react';
import dynamic from 'next/dynamic';
import NProgress from 'nprogress';
import { connect } from 'react-redux';
import Router from 'next/router';
import {
actionFinansial,
actionKarierSukses,
actionGayaHidup
} from '/actions/navbar/navbar';
import SearchBar from '/components/Navbar/SearchBar';
import Nav from '/components/Navbar/Nav';
const Content = dynamic(import('/components/Navbar/Content'));
Router.onRouteChangeStart = url => {
console.log(`Loading: ${url}`);
NProgress.start();
};
Router.onRouteChangeComplete = () => NProgress.done();
Router.onRouteChangeError = () => NProgress.done();
class Navbar extends Component {
state = {
isClick: false,
active: ''
};
componentDidMount() {
document.addEventListener('click', this.handleClickOutside);
}
componentWillUnmount() {
document.addEventListener('click', this.handleClickOutside);
}
handleClickOutside = e => {
if (this.nav && !this.nav.contains(e.target)) {
this.resetNavbar();
}
};
resetNavbar = () => {
this.setState({
isClick: false,
active: ''
});
};
handleClick = (e, data) => {
e.preventDefault();
const cond = data === e.target.className || data === e.target.id;
if (cond) {
this.setState({
isClick: true,
active: data
});
}
if (data === this.state.active) {
this.setState({
isClick: false,
active: ''
});
}
};
handleContent = id => {
if (this.state.active === 'Gaya Hidup') {
this.props.fetchGayaHidup(id);
} else if (this.state.active === 'Finansial') {
this.props.fetchFinansial(id);
} else {
this.props.fetchKarierSukses(id);
}
};
renderArrow = (type, data) => {
return <span id={type} className={data} onClick={this.handleClick} />;
};
render() {
const karierSukses = {
16: 'Karier',
15: 'Sukses',
10: 'Edukasi'
};
const gayahidup = {
16: 'Hiburan',
17: 'Jalan-Jalan',
18: 'Tekno',
19: 'Hobi',
8: 'Hubungan',
9: 'Pernikahan',
14: 'Keluarga',
12: 'Kesehatan',
15: 'Rumah',
11: 'Otomotif'
};
const finansial = { 16: 'Bisnis', 11: 'Keuangan', 12: 'Investasi' };
const {
imgBig,
contentFinansial,
contentKarierSukses,
contentGayaHidup
} = this.props;
const { active, isClick } = this.state;
return (
<section className="navbar" ref={node => (this.nav = node)}>
{/* Category Nav */}
<Nav
resetNavbar={this.resetNavbar}
active={active}
handleClick={this.handleClick}
arrow={this.renderArrow}
/>
{/* Category Content */}
{this.state.active === 'Gaya Hidup' ? (
<Content
imgBig={imgBig}
categoriesIds={gayahidup}
handleContent={this.handleContent}
active="8"
activeName="Hubungan"
content={contentGayaHidup}
/>
) : (
''
)}
{this.state.active === 'Finansial' ? (
<Content
imgBig={imgBig}
icClick={isClick}
categoriesIds={finansial}
handleContent={this.handleContent}
active="11"
activeName="Keuangan"
content={contentFinansial}
/>
) : (
''
)}
{this.state.active === 'Karier & Sukses' ? (
<Content
imgBig={imgBig}
icClick={isClick}
categoriesIds={karierSukses}
handleContent={this.handleContent}
active="10"
activeName="Edukasi"
content={contentKarierSukses}
/>
) : (
''
)}
{this.state.active === 'search-bar' ? <SearchBar /> : ''}
<div className="navbar__search-icon">{/* <img src="" alt="" /> */}</div>
</section>
);
}
}
function mapStateToProps(state) {
return {
contentFinansial: state.navbar.category.finansial,
contentKarierSukses: state.navbar.category.karierSukses,
contentGayaHidup: state.navbar.category.gayaHidup
};
}
export default connect(mapStateToProps, {
fetchGayaHidup: actionGayaHidup,
fetchFinansial: actionFinansial,
fetchKarierSukses: actionKarierSukses
})(Navbar);