高阶组件看不到道具

时间:2019-02-21 16:37:03

标签: javascript reactjs react-native redux react-redux

我正在使用react-native和redux。错误来自redux存储中的组件。之后,我收到:渲染未返回任何内容。这通常意味着缺少return语句。或者,要不显示任何内容,请返回null。

这有什么问题?为什么特设看不见道具?

我的组件

import React, { Component } from 'react';
import withHandleError from './withHandleError';

class SendScreen extends Component {

  render() {
    const { error } = this.props;
    return (
    <div> Test </div>
    )
  }
};

const mapStateToProps = ({ppm}) => ({
  error: ppm.error
})


export default withHandleError(connect(mapStateToProps)(SendScreen));

还有HoC:

import React, { Component } from 'react';
import { ErrorScreen } from '../../ErrorScreen';
import { View } from 'react-native';

export default Cmp => {
  return class extends Component {
    render() {
      const {error, ...rest } = this.props;
      console.log(error)  //// undefined....
      if (error) {
        return <ErrorScreen />
      }

      return <Cmp { ...rest } />
    }
  }
}

1 个答案:

答案 0 :(得分:0)

当您要访问彼此提供的道具时,称为HOC的顺序很重要。重新排序您的connect和withHandleError HOC将起作用

import React, { Component } from 'react';
import withHandleError from './withHandleError';

class SendScreen extends Component {

  render() {
    const { error } = this.props;
    return (
    <div> Test </div>
    )
  }
};

const mapStateToProps = ({ppm}) => ({
  error: ppm.error
})


export default connect(mapStateToProps)(withHandleError(SendScreen));