[英]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.