简体   繁体   English

如何解决警告“预期在此函数array-callback-return中返回值”

[英]How to fix warning “ Expected to return a value in this function array-callback-return ”

I am creating this customized Radio button with React, I checked several times and am pretty sure that I return tags within the map, why would this warning occur? 我正在用React创建这个自定义的单选按钮,我检查了几次,并且非常确定我返回了地图中的标签,为什么会出现此警告?

-- console -- - 安慰 -

webpackHotDevClient.js:198 ./src/toolbox/Radio.js webpackHotDevClient.js:198 ./src/toolbox/Radio.js

C:...\\src\\toolbox\\Radio.js 12:46 warning Expected to return a value in this function array-callback-return C:... \\ src \\ toolbox \\ Radio.js 12:46警告期望在此函数中返回值array-callback-return

✖ 1 problem (0 errors, 1 warning) problem 1个问题(0个错误,1个警告)

-- console -- - 安慰 -

import React from 'react';
import './Radio.less';

const getValueTextPair = (myProps) => {
    let obj;
    switch (Object.prototype.toString.apply(myProps.valTxt)) {
        case '[object String]':
            obj = { 0: myProps.valTxt };
            break;
        case '[object Array]':
            obj = {};
            myProps.valTxt.map((text, index) => {
                obj[index] = text;
            });
            break;
        default:  // [object Object]
            obj = myProps.valTxt;
            break;
    }
    return obj;
}

// to generate radio buttons
const genRd = (myProps) => {
    if (myProps.valTxt === undefined || myProps.valTxt === null) return null;
    let obj = getValueTextPair(myProps);

    return Object.keys(obj).map((index) => {
        return <li className='lird' key={index}>
            <label className='lblBtn'>
                <input type='radio' className='rd' name={myProps.name} value={index} />
                <div className='wrap'>
                    <span className='dot'></span>
                </div>
                <div className='rdText'>{obj[index]}</div>
            </label>
        </li>
    });
}

const Radio = (props) => {
    // props: 
    const myProps = {
        ...props,
        valTxt: props.valTxt,
        name: props.name,
        id: props.id
    };

    // to return a group of radio button
    return <ul id={myProps.id} className='radioBtn'>
        {genRd(myProps)}
    </ul>
}

export default Radio; 

The problem is 问题是

myProps.valTxt.map((text, index) => {
  obj[index] = text;
});

.map should only be used for creating a new array. .map应该仅用于创建新数组。 If you want to perform side-effects , you should use forEach instead: 如果要产生副作用 ,则应改用forEach

myProps.valTxt.forEach((text, index) => {
  obj[index] = text;
});

暂无
暂无

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

相关问题 如何修复警告“预计在箭头 function 数组回调返回中返回一个值” - How fix warning “Expected to return a value in arrow function array-callback-return” 如何修复“预期在箭头函数数组回调返回中返回一个值”? - How to fix "Expected to return a value in arrow function array-callback-return"? 正确警告:“预期在箭头函数数组回调返回中返回一个值” - Correct warning : "Expected to return a value in arrow function array-callback-return " 预期在箭头函数array-callback-return中返回一个值 - Expected to return a value in arrow function array-callback-return 反应警告 预期在箭头函数数组-回调-返回结束时返回一个值 - react warning Expected to return a value at the end of arrow function array-callback-return 预期在箭头函数结束时返回一个值:array-callback-return - Expected to return a value at the end of arrow function: array-callback-return 预期在箭头函数array-callback-return的末尾返回一个值 - Expected to return a value at the end of arrow function array-callback-return 如何解决这个警告警告 Array.prototype.map() 期望从箭头函数数组回调返回的返回值? - How fix this warrning warning Array.prototype.map() expects a return value from arrow function array-callback-return? React:箭头函数需要返回值array-callback-return - React: Arrow function expected a return value array-callback-return 为什么我收到此警告 159:35 警告预计将在箭头 function 数组回调返回的末尾返回一个值? - Why i am getting this warning 159:35 warning Expected to return a value at the end of arrow function array-callback-return?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM