简体   繁体   English

为什么我收到错误data.findIndex不是我的React with Redux项目中的函数?

[英]Why am I getting the error data.findIndex is not a function in my React with Redux project?

I have an ASP.NET Core project with React and Redux, I'm also using the Kendo React UI. 我有一个带有React和Redux的ASP.NET Core项目,我也在使用Kendo React UI。 I'm trying to return data to one of my Kendo widgets but I'm getting an error when I try to do so and I need help identifying what I've done wrong. 我正在尝试将数据返回到我的Kendo小部件之一,但是当我尝试这样做时遇到错误,我需要帮助确定我做错了什么。

When I run my application I get the error of: 当我运行我的应用程序时,出现以下错误:

1 of 2 errors on the page TypeError: data.findIndex is not a function DropDownList/_this.renderDropDownWrapper C:/Users/Allan/node_modules/@progress/kendo-react-dropdowns/dist/es/DropDownList/DropDownList.js:83 TypeError页面上的2个错误中的1个:data.findIndex不是一个函数DropDownList / _this.renderDropDownWrapper C:/Users/Allan/node_modules/@progress/kendo-react-dropdowns/dist/es/DropDownList/DropDownList.js:83

80 | 80 | var focused = _this.state.focused; var focus = _this.state.focused; 81 | 81 | var opened = _this.props.opened !== undefined ? var open = _this.props.opened!==未定义? _this.props.opened : _this.state.opened; _this.props.opened:_this.state.opened; 82 | 82 | var value = _this.value; var value = _this.value;

83 | 83 | var selectedIndex = data.findIndex(function (i) { return areSame(i, value, dataItemKey); }); var selectedIndex = data.findIndex(function(i){return areSame(i,value,dataItemKey);}); 84 | 84 | var text = getItemValue(value, textField); var text = getItemValue(value,textField); 85 | 85 | var valueDefaultRendering = (React.createElement("span", { className: "k-input" }, text)); var valueDefaultRendering =(React.createElement(“ span”,{className:“ k-input”},text)); 86 | 86 | var valueElement = valueRender !== undefined ? var valueElement = valueRender!==未定义?

In the console this error shows as: 在控制台中,此错误显示为:

Warning: Failed prop type: Invalid prop data of type string supplied to DropDownList , expected array . 警告:prop类型失败:提供给DropDownList string类型无效的prop data ,期望array

The error makes sense, but the data I'm returning should be an array. 该错误是有道理的,但是我返回的数据应该是一个数组。 It's not though as it doesn't appear to return anything. 不是因为它似乎没有返回任何东西。 So I've done something wrong. 所以我做错了。

Here is my code so far, please note that my data is served from a generic repository. 到目前为止,这是我的代码,请注意,我的数据是从通用存储库提供的。

components/vessels/WidgetData.js 组件/容器/ WidgetData.js

import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { actionCreators } from '../../store/Types';
import { DropDownList } from '@progress/kendo-react-dropdowns';

class WidgetData extends Component {
    state = {        
        vesseltypes: ""
    };
    componentWillMount() {
        this.props.requestTypes();        
    }
    render() {            
        return (            
            <div>    
                <DropDownList data={this.state.vesseltypes} />
            </div>
        );
    }
}
export default connect(
    state => state.vesseltypes,
    dispatch => bindActionCreators(actionCreators, dispatch)
)(WidgetData);

components/store/Types.js 部件/存储/ Types.js

const requestVesselTypes = 'REQUEST_TYPES';
const receiveVesselTypes = 'RECEIVE_TYPES';
const initialState = {
    vesseltypes: [],
    isLoading: false
};

export const actionCreators = {
    requestTypes: () => async (dispatch) => {
        dispatch({ type: requestVesselTypes });

        const url = 'api/KendoData/GetVesselTypes';
        const response = await fetch(url);
        const alltypes = await response.json();

        dispatch({ type: receiveVesselTypes, alltypes });
    }   
}
export const reducer = (state, action) => {
    state = state || initialState;

    if (action.type === requestVesselTypes) {
        return {
            ...state,
            isLoading: true
        };
    }
    if (action.type === receiveVesselTypes) {
        alltypes = action.alltypes;
        return {
            ...state,
            vesseltypes: action.alltypes,
            isLoading: false
        }
    }    
    return state;
};

And finally, the reducer is defined in the store 最后,reducer在商店中定义

components/store/configureStore.js 部件/存储/ configureStore.js

const reducers = {
    vesseltypes: Types.reducer
};

I've tested the API to ensure data is there and it works, I've logged said data to the console from Types.js in the store and I can see it's returned. 我已经测试过API以确保数据在那里并且可以正常工作,我已经从Types.js中的Types.js将所述数据记录到控制台中,并且可以看到返回了数据。 I'm very much new to react with redux so I'm trying to find my way here and any help is appreciated. 我对redux的反应非常陌生,因此我尝试在这里找到自己的出路,感谢您的帮助。

You need to remove the following state definition, since you want to refer to the value in the redux store, not to a local value: 您需要删除以下状态定义,因为您要引用redux存储中的值,而不是本地值:

class WidgetData extends Component {
    state = {        
        vesseltypes: ""
    };

Then, in your code, you need to refer to the redux store value: this.props.vesseltypes : 然后,在您的代码中,您需要引用redux存储值: this.props.vesseltypes

class WidgetData extends Component {
    state = {        
        vesseltypes: ""
    };
    componentWillMount() {
        this.props.requestTypes();        
    }
    render() {            
        return (            
            <div>    
                <DropDownList data={this.props.vesseltypes} />
            </div>
        );
    }
}

And you need to change the connect definition: 并且您需要更改连接定义:

export default connect(
    vesseltypes => state.vesseltypes,
    dispatch => bindActionCreators(actionCreators, dispatch)
)(WidgetData);

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

相关问题 为什么我的 Route Redux 代码中出现错误? - Why am I getting an error in my Route Redux code? 为什么我在 React 和 redux 上收到此错误“TypeError: state is not iterable” - Why am i getting this error 'TypeError: state is not iterable' on react and redux 为什么我在系统中第一次运行 react js 项目时收到此错误消息? - Why I am getting this error message when I run react js project first time in my system? 为什么我无法 map 我在 React 项目中的数据? - Why am I unable to map my data in React project? 为什么我在 React Redux 减速器中收到关于合成事件的警告? - Why am I getting a warning about synthetic events in my React Redux reducer? 我的地图函数返回 React 组件时出错 - I am getting an error in my map function returning React component 为什么我的对象方法出现“不是函数错误”? - why am I getting “is not a function error” for my object method? 为什么我得到控制台错误 findIndex() is not a function - Why do I get console error findIndex() is not a function 我的findOutlier函数怎么了? 为什么我会出错? - what is wrong with my findOutlier function? why am i getting error? Javascript - 为什么我的功能出现NAN错误 - Javascript - Why am I Getting NAN error with my function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM