- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在 github 上理解这段代码
程序员在这里使用了类似的东西
constructor(props) {
super(props);
this.actions = bindActionCreators(Actions, this.props.dispatch);
}
问题 1:这里,this.actions = bindActionCreators(Actions, this.props.dispatch);
对我来说没有意义
此外,这不是主要问题,但从我学习 React-redux 的地方,我们曾经像这样连接组件
export default connect(mapStateToProps,
{fetchCoin,
updateCrypto,
CurrencyRate,
CurrencyState
})(cryptoTicker);
在上面的代码片段中,程序员做了这样的事情
@connect(state => {
return {
score: state.game.get("score"),
result: state.game.get("result")
};
})
问题 2:** 这对我来说又有点陌生(就像我有几个月使用 js 的经验,但这是我第一次遇到 **@
) 那么有人可以向我解释一下 JS 中的 @ 是什么吗?
最后,他声明了两个在类的返回语句之后调用 onclick
的函数
_handleNewGame = () => {
this.actions.initGame();
}
/**
* Save the current game`s state.
*/
_handleSaveGame = () => {
this.actions.saveGame();
}
现在由于我对 JS 和 React 还比较陌生,所以这部分我有两个问题
问题 3:我们可以在 return 和 render 之后在类中创建一个函数吗?如果是,那么在声明所有其他函数的位置渲染之前创建一个函数(通常)不是一个好习惯吗?
最佳答案
对于问题 #1 和 #2,您的“@connect”和“connect(mapStateToProps, ...)(cryptoTicker)”语法是等效的。 @ 向解析器表明我们正在为我们的函数使用装饰器,即 connect(...) 函数,并输出相同的类,在本例中为 Game,但具有附加功能。这称为 HoC(高阶组件)设计模式,您可以在此处阅读更多相关信息 https://medium.com/@mappmechanic/react-utility-higher-order-components-as-decorators-tc39-stage-2-9e9f3a17688a
对于问题 #3,你是对的,最好将渲染函数留给组件中的最后一个函数。
关于javascript - bindActionCreators,React-Redux 中的@connect 和理解代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53310490/
我似乎找不到这个问题的具体答案。 如果我有一个连接的组件,并且我想从不同的 reducer 传递多个 Action : import { actions as commonActions } from
我的印象是 bindActionCreators 的目的是将 actionCreators 包装在一个调度函数中,并为这些新函数命名(可以传递给组件作为 Prop ,通过 mapDispatchToP
我很难理解 mapDispatchToProps 这里使用了一个 mapDispatchToProps 函数 const mapDispatchToProps = () => { return {
我正在阅读这篇文章,我注意到他没有使用 redux 附带的 bindActionCreators 方法,这是为什么? 我不需要吗? 这是帖子:https://medium.com/@stowball/
经过 20 个小时的测试,我不明白我错在哪里。 如果我使用 storeredux dispacth,一切都可以正常工作,但现在我将使用带有 bindActionCreators 的操作 这是我的行动
我对JavaScript和react-native还是很陌生,并且已经有需要添加功能的现有项目。它使用redux和redux-thunk和redux-saga发送API请求。当前,每个组件仅支持1个d
我正在玩 redux,我开始收到此错误,无法解决它,我希望它有任何意义,但似乎找不到任何信息..任何人都可以解释原因这可能表明?谢谢.. 代码: import React, { Component }
我试图在 github 上理解这段代码 程序员在这里使用了类似的东西 constructor(props) { super(props); this.actions = bindAc
我见过很多在 mapDispatchToProps 函数中使用 bindActionCreators 函数的示例,如下所示: ... const mapDispatchToProps = (dispa
bindActionCreators expected a function actionCreator for key 'ACCOUNT_LOGIN', instead received type
我正在尝试在 React 中创建一个产品列表,我可以在其中添加和删除产品。 我开始研究如何使用 redux 来做到这一点框架/平台和 native react 我已经有一个函数 productList
我已经按照以下方式在一个类中定义了 Redux Action 创建者: export class ActionCreator { login() { return { type
我是一名优秀的程序员,十分优秀!