【发布时间】:2018-11-16 12:17:55
【问题描述】:
在一页上我有两个组件。一个组件从 redux 中设置的 api 获取值,兄弟组件需要访问状态。但是兄弟组件从未调用过 mapStateToProps 函数。
在歌曲节点的redux中获取数据的代码:
Songs.propTypes = {
topTrendingSongs: PropTypes.object,
song: PropTypes.object,
getHomeTopTrendingSongs: PropTypes.func.isRequired,
getUrls: PropTypes.func.isRequired,
getSong: PropTypes.func.isRequired
}
function mapStateToProps(state) {
console.log('Type IS : ', typeof state.songsReducer.song_one);
return {
data: state.homeReducer.topTrendingSongs,
song: state.songsReducer.song
}
}
export default connect(mapStateToProps, { getUrls, getHomeTopTrendingSongs, getSong })(Songs);
从未在兄弟组件中调用过以下调用:
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import playerIcon from '../img/playerIcon.svg';
import Pause from '../img/pause.svg';
import { connect } from 'react-redux';
export class Player extends Component {
constructor(props) {
super(props);
this.state = {
player: false,
cSong: JSON.parse(localStorage.getItem('CurrentSong'))
}
}
componentDidMount() {
console.log('current song data', this.props.song);
}
render() {
return (
<div id="min-player" className="min-player">
<video id="video"></video>
</div>
);
}
}
function mapStateToPropss(state) {
console.log('all state', state);
return {
song: state.songsReducer.song
}
}
export default connect(mapStateToPropss)(Player);
【问题讨论】:
-
那么问题是mapStateToProps 没有在Player 上被调用?如果没有看到该组件的其余部分,可能很难提供帮助。您是否已经在组件声明中使用了“导出默认值”?您可以将整个 Player 组件的代码添加到您的问题中吗?
-
@CodyParker 请检查播放器代码。任何帮助都会节省时间。谢谢
-
问题是“您是否已经在组件声明中使用“导出默认值””,正如您所说,谢谢,伙计。
-
与此有关。真高兴你做到了。我也添加了更多细节作为答案。如果对您有帮助,请随时接受。
标签: reactjs redux react-redux redux-thunk