reactjs - 如何在 React-Recompose 应用程序中将事件处理程序传递给 React-node
问题描述
在以下位置获得工作应用程序:https ://github.com/BeerDRinker/recompose-ref
以下代码(/src/App.js 中的注释部分)按预期工作:
class App extends Component {
constructor(props) {
super(props);
this.node = React.createRef();
this.state = {
value: 1
};
}
handleTouchStart = e => {
e.preventDefault();
this.setState({ value: this.state.value + 1 });
};
handleTouchEnd = e => {
e.preventDefault();
this.setState({ value: this.state.value - 1 });
};
componentDidMount() {
this.node.current.ontouchstart = this.handleTouchStart;
this.node.current.ontouchend = this.handleTouchEnd;
}
render() {
return (
<div>
<h3>Value: {this.state.value}</h3>
<button ref={this.node}>Submit</button>
</div>
);
}
}
export default App;
但是我需要使用 Recompose 来获得相同的功能。我试过了,但没有任何效果。我的代码示例(在 /src/App.js 中未注释部分)不起作用:
import React from "react";
import {
compose,
lifecycle,
setDisplayName,
withProps,
withStateHandlers
} from "recompose";
import "./App.css";
const state = {
value: 1
};
const stateHandlers = {
handleTouchStart: value => () => ({
value: value + 1
}),
handleTouchEnd: value => () => ({
value: value - 1
})
};
export const enhance = compose(
setDisplayName("App"),
withProps(props => ({
bookNode: React.createRef()
})),
withStateHandlers(state, stateHandlers),
lifecycle({
componentDidMount() {
this.bookNode.current.ontouchstart =
this.handleTouchStart;
this.bookNode.current.ontouchend = this.handleTouchEnd;
}
})
);
export const App = ({ value, bookNode }) => (
<div>
<h3>Value: {value}</h3>
<button ref={bookNode}>Submit</button>
</div>
);
export default enhance(App);
刚开始使用recompose,很多东西对我来说仍然很神奇))我希望一些可以帮助我,通过几天来解决这个问题。
解决方案
组合组件存在问题。
上没有bookNode
和事件处理程序this
。App
是无权访问的无状态组件,this
事件bookNode
处理程序是道具。
顾名思义,它不是value
传递给状态处理程序的,而是。state
它应该是:
const stateHandlers = {
handleTouchStart: state => () => ({
value: state.value + 1
}),
handleTouchEnd: state => () => ({
value: state.value - 1
})
};
export const enhance = compose(
setDisplayName("App"),
withProps(props => ({
bookNode: React.createRef()
})),
withStateHandlers(state, stateHandlers),
lifecycle({
componentDidMount() {
this.props.bookNode.current.ontouchstart = this.props.handleTouchStart;
this.props.bookNode.current.ontouchend = this.props.handleTouchEnd;
}
})
);
export const App = ({ value, bookNode }) => (
<div>
<h3>Value: {value}</h3>
<button ref={bookNode}>Submit</button>
</div>
);
这是一个演示。
通常没有理由手动访问 DOM 来设置事件,因为 React 会处理这个。这消除了对 ref 和生命周期钩子的需要:
export const enhance = compose(
setDisplayName("App"),
withStateHandlers(state, stateHandlers)
);
const App = ({ value, handleTouchStart, handleTouchEnd }) => (
<div>
<h3>Value: {value}</h3>
<button onTouchStart={handleTouchStart} onTouchEnd={handleTouchEnd}>Submit</button>
</div>
);
推荐阅读
- android - 有没有办法获取最新的 TalkBack 源代码?
- c - [strcat&malloc&free],当我使用strcat()时,即使我故意让它成为指针,指针似乎也不是NULL(没有额外的赋值)?
- python - 如果我提升力量,如何使我的力量统计增加多少计数器增加
- javascript - 从下拉列表加载图像,然后在用户根据所选图像单击时播放声音
- mysql - mysql - 如何限制 SELECT 查询中的某些记录
- postgresql - API 仅返回嵌套结构的最后一行
- json - 使用版本控制在项目之间共享 Json Schema 文件
- android - 当我尝试从 Android 中的 facebook 提取数据时获取 NetworkOnMainThreadException
- firebase-realtime-database - 如何检查 auth.uid 是否与数据库中的字段匹配
- google-app-maker - 查询服务器脚本中的自定义参数错误