简体   繁体   English

无法访问由Redux MapStateToProps修改的React道具

[英]Can't access React props modified by Redux MapStateToProps

Got undefined when trying to access this.props.state value 尝试访问this.props.state值时未定义

import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import * as actions from '../actions';

const mapStateToProps = ({ movies }) => ({
  state: movies,
});

class App extends Component {

  componentDidMount() {
    this.props.addMovie({ title: 'Movie' }); //redux action
  }

  render() {
    const { state } = this.props;
    console.log(state.test);       //Object {title: "Movie"}
    console.log(state.test.title); //Uncaught TypeError: Cannot read property 'title' of undefined
    return (
      <div>Hello {state.test.title}!</div> //doesn't work
    );
  }
}

export default connect(mapStateToProps, actions)(App);

After redux complete the action i've got state object in component props 在redux完成动作之后,我在组件道具中有了状态对象

Redux开发工具

I can access it from Chrome DevTools $r.props.state.test.title but can't access it from render function 我可以从Chrome DevTools $r.props.state.test.title访问它,但不能从渲染功能访问它

For example I can get value from this.props.params.movieID but can't get any params modified by mapStateToProps 例如,我可以从this.props.params.movieID获取值,但无法获取由mapStateToProps修改的任何参数

How can I put this.props.state.test.title value in my div? 如何将this.props.state.test.title值放入div?

Inside your render function check if the title is undefined because the render function is being called before your redux action is resolving. 在渲染函数内部,检查标题是否未定义,因为在解析redux操作之前已调用了渲染函数。 Once your state is updated and your component renders it should show up the second time. 更新状态并渲染组件后,状态将第二次显示。

render() {
  const { state } = this.props;
  console.log(state.test);
 if(state.test && state.test.title){
   console.log(state.test.title); 
    return (
     <div>Hello {state.test.title}!</div>
   )
} else {
   return (
    <div>No movies listed</div>
  );
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React/Redux - 无法将存储值发送到组件的道具(mapStateToProps) - React/Redux - Can't send a store value into props of a component (mapStateToProps) 反应 redux mapStateToProps - 无法访问 state 作为道具 - React redux mapStateToProps - cannot access state as props React / Redux:mapStateToProps实际上并没有将状态映射到props - React / Redux: mapStateToProps not actually mapping state to props React Redux不同步mapStateToProps中的道具 - React redux do not sync props in mapStateToProps React redux mapStateToProps 未设置返回到我的道具 - React redux mapStateToProps not set return to my props 添加反应路由后无法访问redux存储。 未调用 MapStateToProps - Can't access redux store after adding react routing. MapStateToProps is not being invoked React Native Redux:错误无法找到变量mapStateToProps - React Native Redux: Error Can't find variable mapStateToProps connect(mapStateToProps,{function})没有为此this设置功能。props-React / Redux - connect(mapStateToProps, { function }) isn't setting function to this.props - React / Redux Redux的mapStateToProps不在AngularJS和React之间映射道具 - Redux's mapStateToProps not mapping props between AngularJS and React 链接redux连接mapStateToProps,以访问mapDispatchToProps内部的道具 - Chaining redux connect mapStateToProps , to access props inside mapDispatchToProps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM