javascript – 在兄弟姐妹之间存储活动状态
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 在兄弟姐妹之间存储活动状态,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3407字,纯文字阅读大概需要5分钟。
内容图文
我在两个兄弟组件之间使用活动状态时遇到了困难.我有NavComponent.jsx& HeaderComponent.jsx,它们都呈现给DOM中的不同区域.
我有一个汉堡包按钮,切换活动状态,使其变为X,同时将菜单状态设置为活动以及导航.我的任务是更改菜单的交互以在菜单打开时将内容推到一边,这意味着我需要打破标题并导航到DOM中的不同组件.现在,当我希望它们一起工作时,活动状态彼此独立地工作.
有人告诉我要使用Redux商店,但也无法使用.
非常感谢帮助.
NavComponent.jsx
import React from 'react';
const Navigation = (props) => (
<nav className={'navigation' + (props.active ? ' slide-in' : '')}>
<ul className="nav">
{
props.items.map(
(item, idx) => {
return (
<NavigationLink key={idx} href={item.href} text={item.text} clickHandler={props.clickHandler} />
);
}
)
}
</ul>
</nav>
);
export default class NavComponent extends React.Component {
constructor (props) {
super(props);
this.state = {
active: false
};
this.navItems = [
{
href: '/app/page1',
text: 'PAGE 1'
},
{
href: '/app/page2',
text: 'PAGE 2'
},
{
href: '/app/page3',
text: 'PAGE 3'
}
];
}
handleClick (e) {
const viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
if (viewportWidth <= 767) {
this.setState({ active: !this.state.active });
}
}
render () {
return (
<Navigation active={this.state.active} items={this.navItems} clickHandler={this.handleClick.bind(this)} />
);
}
}
HeaderComponent.jsx
import React from 'react';
import Logo from '../img/logo.png';
import Logo2x from '../img/logo@2x.png';
import Logo3x from '../img/logo@3x.png';
const HamburgerToggle = (props) => (
<button className={'hamburger hamburger--squeeze' + (props.active ? ' is-active' : '')} onClick={props.clickHandler} type="button">
<span className="hamburger-box">
<span className="hamburger-inner"></span>
</span>
</button>
);
const BrandLogo = () => (
<a href="/app/page1" className="logo-link">
<img width="92" height="29" src={Logo} srcSet={Logo + ' 1x, ' + Logo2x + ' 2x, ' + Logo3x + '3x'} alt="Logo" className="logo" />
</a>
);
export default class HeaderComponent extends React.Component {
constructor (props) {
super(props);
this.state = {
active: false
};
}
handleClick (e) {
const viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
if (viewportWidth <= 767) {
this.setState({ active: !this.state.active });
}
}
render () {
return (
<div>
<HamburgerToggle active={this.state.active} clickHandler={this.handleClick.bind(this)} />
<BrandLogo />
</div>
);
}
}
解决方法:
store.js
const defaultState = {
headerData
};
const store = createStore(rootReducer, defaultState));
export default store;
rootReducer这里是一个.js文件,其中所有的reducer组合在一起.
HeaderComponent.js
class HeaderComponent extends React.Component {
constructor...
handleClick...
render...
}
function mapStateToProps (state) {
return {
headerData: state.headerData
};
}
export default connect(mapStateToProps)(HeaderComponent);
NavComponent.js
class NavComponent extends React.Component {
constructor...
handleClick...
render...
}
function mapDispatchToProps (dispatch) {
return bindActionCreators(navActions, dispatch);
}
export default connect(mapDispatchToProps)(NavComponent);
在这种情况下,您的NavComponent可以访问文件navActions中的所有操作.您需要为此创建一个js文件navActions.
在store.js中创建Instant时,You HeaderComponent将映射到商店的headerData.您需要为此创建.js文件headerData.
由NavComponent中调用的操作触发的reducer将更新store中的headerData,并且HeaderComponent将重新呈现,因为它映射到它.
我强烈建议您阅读Redux Documentation中的全部基础知识,以了解如何编写动作和减速器.
内容总结
以上是互联网集市为您收集整理的javascript – 在兄弟姐妹之间存储活动状态全部内容,希望文章能够帮你解决javascript – 在兄弟姐妹之间存储活动状态所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。