TypeError:无法读取null

时间:2019-03-22 00:39:01

标签: javascript reactjs react-redux

  

TypeError:无法读取null的属性“ searchField”   App.render

entire error report rest of project files in comments

2 个答案:

答案 0 :(得分:0)

  

TypeError:无法读取null的属性“ searchField”

意味着编译器在您的组件中找不到state

您想从redux检索此值,因此必须使用 this.props代替this.state

答案 1 :(得分:0)

如我所见,您要使用的所有道具都来自商店(通过mapStateToProps),因此不是

const { searchField, onSearchChange, robots, isPending } = this.state;

你应该做

const { searchField, onSearchChange, robots, isPending } = this.props;

此外,如果您在渲染器中使用状态,请不要忘记在组件或构造函数中初始化状态

// if your state doesn't depend on props    
class App extends Component {
         state = {
           isLoading: false
         }
    ....



//if your state depends on props(rare case but sometimes useful)

class App extends Component {
   constructor(props) {
      super(props);

      const {isLoading} = props;
      this.state = {
         isLoading
      }
    }
....