简体   繁体   English

如何修复错误:“您必须将组件传递给连接返回的 function。 而是在我的 React-Redux 应用程序中收到 {}”?

[英]How to fixed error: “You must pass a component to the function returned by connect. Instead received {}”, in my React-Redux app?

I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:I have error:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:我有错误:
[![enter image description here][2]][2] [![在此处输入图像描述][2]][2]

App.js(components): App.js(组件):

import React from 'react';
import { Component } from 'react';
import { connect } from 'react-redux'
import { fetchData } from '../actions';
import TableData from "./TableData";
import TableSearch from "./TableSearch";

export function searchFilter (search, data) {                                               
  return data.filter(n => n.term.toLowerCase().includes(search));
}


export const days = ["23-08-2019", "24-08-2019", "25-08-2019"];        

class Root extends React.Component {

  componentDidMount() {
    this.props.onFetchData(this.props.day);
  }

  render() {
    const { search, shift, data, filteredData, onFilter, onSetSearch, onFetchData } = this.props;

   return (
      <div>
        <TableSearch value={search}
          onChange={(e) => onSetSearch(e.target.value)} 
          onSearch={() => onFilter()} />

        {days && days.map((day, i) => (
          <button key={day} 
            onClick={() => onFetchData(day)}
            className={i === day ? "active" : ""}>{day}</button>
        ))}
        <br />
        {data && Object.keys(data).map(n => (
          <button data-shift={n}
            onClick={(e) => onFilter({ shift: e.target.dataset.shift })}
            className={n === shift ? "active" : ""}>{n} shift</button>
        ))}

        {data && <TableData data={filteredData} /> }

      </div>
    );
  }

}

export const ConnectedRoot = connect(             
  (state) => state,
  (dispatch) => ({
    onFilter: (args) => dispatch({ type: 'RUN_FILTER', ...args }),
    onSetSearch: (search) => dispatch({ type: 'SET_SEARCH', search }),
    onFetchData: (day) => dispatch(fetchData(day))
  })
);



  [1]: https://i.stack.imgur.com/M7Onv.jpg
  [2]: https://i.stack.imgur.com/x0b9i.jpg

Look's like you missed on applying connect to the Root component看起来你错过了将连接应用到 Root 组件

export const ConnectedRoot = connect(             
  (state) => state,
  (dispatch) => ({
    onFilter: (args) => dispatch({ type: 'RUN_FILTER', ...args }),
    onSetSearch: (search) => dispatch({ type: 'SET_SEARCH', search }),
    onFetchData: (day) => dispatch(fetchData(day))
  })
)(Root);

You need to add the (Root) to the end of connect in the ConnectedRoot function您需要将 (Root) 添加到 ConnectedRoot function 中的连接末尾

export const ConnectedRoot = connect(             
  (state) => state,
  (dispatch) => ({
    onFilter: (args) => dispatch({ type: 'RUN_FILTER', ...args }),
    onSetSearch: (search) => dispatch({ type: 'SET_SEARCH', search }),
    onFetchData: (day) => dispatch(fetchData(day))
  })
)(Root);

暂无
暂无

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

相关问题 您必须将一个组件传递给 connect 返回的函数。 而是收到未定义 - You must pass a component to the function returned by connect. Instead received undefined ReactJS您必须将组件传递给connect返回的函数 - ReactJS You must pass a component to the function returned by connect 如何在 react-redux 组件上传递道具? - How to pass props on react-redux component? React-Redux容器在Connect(ModalRoot)中抛出“ mapStateToProps()”时必须返回一个普通对象。 而是收到未定义的信息。” - React-Redux container throws “mapStateToProps() in Connect(ModalRoot) must return a plain object. Instead received undefined.” 在功能组件中,如何使用 react-redux 连接从 redux 商店访问道具? - In a Functional Component, how do you access props from the redux store using react-redux connect? 如何使用react-redux将ReactJS组件正确连接到Redux? - How to properly connect a ReactJS component to Redux using react-redux? 在React-Redux App中访问另一个组件中的Connect函数值时出现问题 - Problems accessing Connect function value in another Component in a React-Redux App 在react-redux中连接错误? - Error with connect in react-redux? 必须返回有效的react元素或null。 react-redux连接 - A valid react element or null must be returned. react-redux connect 如何将componentDidMount()与react-redux connect()混合使用? - How do you mix componentDidMount() with react-redux connect()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM