简体   繁体   English

在一个简单的函数中反应 js 错误,该函数接受一个数组并返回一个随机值

[英]React js error in a simple function that takes an array and returns a random value

I am getting an error that I am not able to figure, its about a function that takes an array and returns a random value from it.我收到一个我无法弄清楚的错误,它是关于一个函数,它接受一个数组并从中返回一个随机值。 I used the same function last time and there was no such error我上次使用了相同的功能,没有出现这样的错误

helpers.js helpers.js

function choice(arr) {
  let randomIndex = Math.floor(Math.random() * 3);
  return arr[randomIndex];
}

export { choice };

box.js盒子.js

import React, { Component } from 'react';
import {choice} from './helpers';
 import './Box.css'

class Box extends Component {
    static defaultProps = {
        allColors: ["purple", "magenta", "violet", "pink"]
    };
    constructor(props){
        super(props);
        this.state = {color: choice(this.props.allcolors)};
        this.handleClick = this.handleClick.bind(this);
    }
    handleClick(){

    }
    render(){
        return(
            <div className="Box" style={{backgroundColor: this.state.color}
            } onClick={this.handleClick}>

            </div>
        );
    }
}

export default Box;

here is the error这是错误在此处输入图片说明

There are two mistakes in your code:您的代码中有两个错误:

  1. this.props.allcolors is undefined , because you have set the prop allColors with uppercase "C" this.props.allcolorsundefined ,因为你已经用大写的“C”设置了道具allColors
  2. This might not be a problem in your specific scenario, but in general, the function choice , won't give you your desired result.在您的特定场景中,这可能不是问题,但总的来说,函数choice不会给您想要的结果。 It now only works correctly for arrays of length 3. To fix this, you could change 3 into arr.length like so:它现在仅适用于长度为 3 的数组。要解决此问题,您可以将3更改为arr.length如下所示:
function choice(arr) {
  let randomIndex = Math.floor(Math.random() * arr.length);
  return arr[randomIndex];
}

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

相关问题 Function 接受一个数组并返回 js 中旋转的 n 个空格内的元素 - Function that takes an array and returns elements inside rotated n spaces in js javascript函数需要字符串并返回数组 - javascript function takes string and returns array 接受通用参数并返回通用值的函数 - Function that takes generic arguments and returns generic value JS/Node:一个函数,它接受一个数字 N 并返回一个值为 [0, 1, ... N-1] 的数组 - JS/Node: A function that takes a number N and returns an array with the values [0, 1, ... N-1] 如何调用一个通过Emscripen / Wasm从JS中按值获取(或返回)结构的C函数? - How do you call a C function that takes (or returns) a struct by value from JS via Emscripen/Wasm? Function 接受输入文本,检查它是否具有某些元素,并返回 output 值 (JS) - Function that takes an input text, checks if it has certain elements, and returns an output value (JS) 带有返回值的函数的数组 - Array with a function that returns a value 简单的JavaScript函数返回函数而不是值 - Simple JavaScript function returns function and not value 如何从某些函数构建c3js折线图,而不仅仅是具有随机数的简单数组? - How to build c3js line diagram from some function, not just simple array with random numbers? 一个简单的 JS 裁剪 function 返回数据 url - A simple JS cropping function that returns data url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM