javascript - React 组件 - 为什么可以在 AsyncApp 组件中调度
问题描述
参考这个例子 https://redux.js.org/advanced/example-reddit-api
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import {
selectSubreddit,
fetchPostsIfNeeded,
invalidateSubreddit
} from '../actions'
import Picker from '../components/Picker'
import Posts from '../components/Posts'
class AsyncApp extends Component {
constructor(props) {
super(props)
this.handleChange = this.handleChange.bind(this)
this.handleRefreshClick = this.handleRefreshClick.bind(this)
}
componentDidMount() {
const { dispatch, selectedSubreddit } = this.props
dispatch(fetchPostsIfNeeded(selectedSubreddit))
}
componentDidUpdate(prevProps) {
if (this.props.selectedSubreddit !== prevProps.selectedSubreddit) {
const { dispatch, selectedSubreddit } = this.props
dispatch(fetchPostsIfNeeded(selectedSubreddit))
}
}
handleChange(nextSubreddit) {
this.props.dispatch(selectSubreddit(nextSubreddit))
this.props.dispatch(fetchPostsIfNeeded(nextSubreddit))
}
handleRefreshClick(e) {
e.preventDefault()
const { dispatch, selectedSubreddit } = this.props
dispatch(invalidateSubreddit(selectedSubreddit))
dispatch(fetchPostsIfNeeded(selectedSubreddit))
}
render() {
const { selectedSubreddit, posts, isFetching, lastUpdated } = this.props
return (
<div>
<Picker
value={selectedSubreddit}
onChange={this.handleChange}
options={['reactjs', 'frontend']}
/>
<p>
{lastUpdated && (
<span>
Last updated at {new Date(lastUpdated).toLocaleTimeString()}.{' '}
</span>
)}
{!isFetching && (
<button onClick={this.handleRefreshClick}>Refresh</button>
)}
</p>
{isFetching && posts.length === 0 && <h2>Loading...</h2>}
{!isFetching && posts.length === 0 && <h2>Empty.</h2>}
{posts.length > 0 && (
<div style={{ opacity: isFetching ? 0.5 : 1 }}>
<Posts posts={posts} />
</div>
)}
</div>
)
}
}
AsyncApp.propTypes = {
selectedSubreddit: PropTypes.string.isRequired,
posts: PropTypes.array.isRequired,
isFetching: PropTypes.bool.isRequired,
lastUpdated: PropTypes.number,
dispatch: PropTypes.func.isRequired
}
function mapStateToProps(state) {
const { selectedSubreddit, postsBySubreddit } = state
const { isFetching, lastUpdated, items: posts } = postsBySubreddit[
selectedSubreddit
] || {
isFetching: true,
items: []
}
return {
selectedSubreddit,
posts,
isFetching,
lastUpdated
}
}
导出默认连接(mapStateToProps)(AsyncApp)
该函数可以使用 this.props.dispatch(fetchPostsIfNeeded(nextSubreddit)) 触发诸如“fetchPostsIfNeeded”之类的动作。但是,它假设与 mapDispatchToProps 有关。所以我可以知道为什么程序运行成功且没有错误的场景
解决方案
欢迎来到 SO。
实际上,connect()
已经为您的组件提供了调度。(https://react-redux.js.org/api/connect#example-usage)
您可以将mapDispatchToProps
其作为一个中间件来创建具有调度的方法。
假设我们有一家带有状态计数器的商店。
{
counter: 0
}
在mapDispatchToProps
,我们可以在这个组件中使用创建方法:
{
increment: ()=>{dispatch({ type: 'EDIT', payload: 1 })},
decrement: ()=>{dispatch({ type: 'EDIT', payload: -1 })}
}
推荐阅读
- html - 菜单(图片)没有填满我的 Joomla 网站的宽度
- python - 在“int”和“NoneType”的实例之间不支持“>”。form.is_valid
- solaris - 在 solaris 中移动文件 - 问题是 /usr/bin/mv: arg list too long
- android - 推送到后台时如何运行 Cordova 应用程序逻辑?
- c - 这是未定义的行为吗?
- bash - 用于替换文件名中的无效 Windows 字符的 Bash 脚本
- python-3.x - Qt:opencv python中的会话管理错误
- f# - 从 IHTMLProp 轻松检索值
- api - 我需要使用 rest 从 Sharepoint 术语库中检索一组术语
- notifications - 当应用程序在 Android 10 中被杀死时,WorkManager 无法工作,尽管在版本 9(Pie)之前工作正常