首页 > 解决方案 > React Redux Props 在变量赋值期间未定义

问题描述

为什么当我将道具分配给 JSX 变量时,它输出为未定义?这是正常的吗?或者我的程序中是否有其他代码会导致此问题?

const { foo } = this.props;
console.log(this.props);
console.log(foo);

输出截图

class Library extends Component {

 componentDidMount() {
    M.AutoInit();
  }
  render() {
    const { foo } = this.props;
    console.log(this.props);
    console.log(foo);
    return <div className="container"></div>;
  }
}
const mapStateToProps = state => {
  return {
    cred: state.cred.tabs
  };
};

标签: reactjsredux

解决方案


您正试图foo. this.props但根据您的日志,this.props似乎不包含任何foo属性。


推荐阅读