You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:StackNavigator与Redux集成报错,Accounts页面无法正常渲染

问题解决思路

嘿,我帮你定位到几个关键问题啦,一个个来看:

1. Redux状态结构不匹配(核心错误原因)

你的reducers/accounts.js里,初始状态是一个包裹着数组的对象:

const initialState = {
  accounts: [
    { title: "Bitcoin Slash Fund", hash: "0x83247jfy344fgg", },
  ],
}

再结合reducers/index.js里的combineReducers({ accounts }),最终你的Redux根状态结构是这样的:

{
  accounts: { // 这是accounts reducer对应的状态节点
    accounts: [/* 你的账户数组 */]
  }
}

但你在accounts.js的mapStateToProps里,直接取了state.accounts——这拿到的是外层那个包含数组的对象,而不是数组本身!对象当然没有map方法,所以才会抛出undefined is not a function的错误。

修复方法:修改mapStateToProps,正确获取数组:

function mapStateToProps (state) {
  // 要取到reducer内部的accounts数组
  return { accounts: state.accounts.accounts }
}

2. 组件注册的拼写错误

在index.ios.js的最后一行:

AppRegistry.registerComponent('TestApp', () => RNRedux)

这里的RNRedux是未定义的变量,你应该注册自己定义的TestApp组件:

AppRegistry.registerComponent('TestApp', () => TestApp)

3. 小细节优化(非核心但建议修正)

在accounts.js的render函数里,你把state = {}写在了render内部,这会导致每次render都重新赋值,不符合React的写法规范。建议移到类属性位置:

export class Accounts extends React.Component {
  state = {}; // 移到类顶部
  static navigationOptions = {
    title: 'Accounts',
  }
  constructor(props) {
    super(props);
  }
  render() {
    const { navigate } = this.props.navigation;
    return (
      <View>
        { this.props.accounts.map((l, i) => (
          <ListItem key={i} title={l.title} subtitle={l.hash} />
        )) }
      </View>
    );
  }
}

先把这几个问题修复,尤其是第一个核心问题,应该就能正常渲染Accounts页面的列表啦!

内容的提问来源于stack exchange,提问作者Seva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:30:36