簡體   English   中英

如何解決警告“預期在此函數array-callback-return中返回值”

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

我正在用React創建這個自定義的單選按鈕,我檢查了幾次,並且非常確定我返回了地圖中的標簽,為什么會出現此警告?

- 安慰 -

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

C:... \\ src \\ toolbox \\ Radio.js 12:46警告期望在此函數中返回值array-callback-return

problem 1個問題(0個錯誤,1個警告)

- 安慰 -

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; 

問題是

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

.map應該僅用於創建新數組。 如果要產生副作用 ,則應改用forEach

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM