【问题标题】:Redux in sibling component is never called?兄弟组件中的 Redux 永远不会被调用?
【发布时间】: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


【解决方案1】:

我认为问题在于您在其声明 (export class Player extends Component) 中导出了该类,并且您再次将 export default 与 react-redux 的连接功能一起使用。

因此,在您的 Songs 组件中,您可能使用花括号导入它,这会导入类,但不会导入带有连接函数的类。如果你不带括号导入,它会导入“默认”导出。

如果您总是要使用 redux 连接组件,您应该从 Songs 组件的导入中删除花括号,并且还可能从类声明中删除“导出”。

【讨论】:

    猜你喜欢
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2013-10-12
    • 2012-03-27
    • 2013-08-29
    • 2013-11-03
    相关资源
    最近更新 更多