4、React对组件的DOM事件监听

下面的代码展示了在React中如何对按钮的点击事件和输入框的输入事件做监听:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>State</title>
	<script type="text/javascript" src="../react-0.13.0/build/react.js"></script>
	<script type="text/javascript" src="../react-0.13.0/build/JSXTransformer.js"></script>
</head>
<body>
	<div id="container"></div>
	<script type="text/jsx">
		var HelloWorld = React.createClass({
			handleClick: function() {
				alert('you click me!');
			},
			handleChange: function(event){
				//通过React.findDOMNode()拿到真实的DOM对象
				var node = React.findDOMNode(this.refs.inputContent);
				node.innerHTML = event.target.value;
			},
			render: function() {
				return (
					<div>
						<button onClick={this.handleClick}>Hello</button>
						<br/>
						<input type="text" onChange={this.handleChange} />
						<span ref="inputContent"></span>
					</div>
				);
			}
		});
		React.render(
			<HelloWorld />,
			document.getElementById('container')
		);
	</script>
</body>
</html>
浏览器中执行结果如下:



点击按钮后,会弹出提示框,在输入框中输入文本后,输入框后面会动态显示出输入的文本。

下面解释以上代码:

在组件的render函数中,创建了一个<button>标签,JSX添加事件监听函数,都是以驼峰命名法,跟html中onclick不同的是,JSX中写作onClick,然后指定处理函数为{this.handleClick},同理,html中的onchange在JSX中写作onChange,后面指定的处理函数为{this.handleChange},在input标签后面,还有一个<span>标签,我们为该标签设置了一个ref属性,该属性是JSX中的属性,在组件中,可以通过this.refs.属性值来获取某个虚拟的dom节点,然后用React.findDOMNode()可以获取真实的DOM节点。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
React中,你可以使用`react-router-dom`库来管理路由。如果你想要监听路由变化并控制组件位置,可以使用`withRouter`高阶组件来实现。 首先,使用`withRouter`高阶组件包装你要监听路由变化的组件。这将会把`history`、`location`和`match`三个路由相关的属性注入到该组件的props中。 ``` import { withRouter } from 'react-router-dom'; class MyComponent extends React.Component { // ... } export default withRouter(MyComponent); ``` 然后,在组件的`componentDidMount`生命周期方法中,添加一个路由变化的监听器,并在监听器中更新组件的位置。 ``` import { withRouter } from 'react-router-dom'; class MyComponent extends React.Component { constructor(props) { super(props); this.state = { position: 0 }; } componentDidMount() { window.addEventListener('popstate', this.handlePopState); } componentWillUnmount() { window.removeEventListener('popstate', this.handlePopState); } handlePopState = () => { const { location } = this.props; const position = location.state && location.state.position || 0; this.setState({ position }); } render() { const { position } = this.state; return ( <div style={{ position: 'absolute', top: `${position}px` }}> {/* ... */} </div> ); } } export default withRouter(MyComponent); ``` 在上面的例子中,我们在组件的state中维护了一个`position`属性,用于控制组件的位置。在`componentDidMount`生命周期方法中,我们添加了一个`popstate`事件监听器,该事件会在浏览器的前进或后退按钮被点击时触发。在`handlePopState`回调函数中,我们通过`location.state`获取到之前保存的位置信息,并将其更新到组件的state中。最后,在组件的render方法中,我们使用组件的state来控制组件的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yubo_725

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值