【发布时间】:2016-12-24 14:02:23
【问题描述】:
我正在使用 Webpack、Redux 和 ReactJS。
目前我在index.html 中设置了以下内容,但我想将其转换为 JSX、ReactJS 组件。这样做的正确和正确方法是什么?
在我的index.html<head/> 中,有一个名为classie.js 的类辅助函数:
<script src="classie.js"></script>
<script>
function init() {
window.addEventListener('scroll', function(e){
var distanceY = window.pageYOffset || document.documentElement.scrollTop,
shrinkOn = 300,
header = document.querySelector("header");
if (distanceY > shrinkOn) {
classie.add(header,"smaller");
} else {
if (classie.has(header,"smaller")) {
classie.remove(header,"smaller");
}
}
});
}
window.onload = init();
</script>
在我的index.html<body/>:
<div id="wrapper">
<header>
<div class="container clearfix">
<h1 id="logo">
Practice Navigation Bar
</h1>
<nav>
<a href="">Button 1</a>
<a href="">Button 2</a>
</nav>
</div>
</header>
</div>
所以把它转换成像下面这样的 ReactJS 组件格式:
//App.js
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import actions from '../redux/actions'
class NavBar extends Component {
render() {
return (
<div>
{/*e.g. What should go in here?*/}
</div>
);
}
}
function mapStateToProps(state) {
return state
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(actions, dispatch)
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(NavBar)
提前谢谢你!
【问题讨论】:
标签: javascript html reactjs redux react-jsx