简体   繁体   English

React.js随机名称生成器给出了未定义或无法编译的信息

[英]React.js random name generator giving undefined or failed to compile

I'm sure I have something incorrect here, could use some help. 我确定这里有不正确的地方,可以使用一些帮助。 I am trying to return a random name from my array called "names" however something I've written isn't working correctly. 我正在尝试从我的数组中返回一个随机名称“ names”,但是我编写的内容无法正常工作。

What I'm trying to do is use getRandomName to randomly reach into the array and return a name. 我正在尝试使用getRandomName随机进入数组并返回一个名称。 I always struggle with arrays and am using this exercise to get more familiar 我一直在为数组而苦恼,并且正在使用此练习来变得更加熟悉

My code in my parent is: 我在父母中的代码是:

import React, { Component } from 'react';
import styled from 'styled-components';
import { ContainLeft } from '../../../helper/comps';
import Moment from 'react-moment';
import harry from '../../../../static/quotes-harry.jpg';
import { names, sayings } from './arrays';

const QuoteBox = styled.div`
  width: 500px;
  height: 250px;
  border: 1px solid white;
  background: black;
  display: flex;
`;

const QuoteHolder = styled.div`
  width: 250px;
  margin: 2em;
`;

const Quote = styled.p`
  color: white;
  width: 200px;
  font-size: 16px;
  font-weight: bold;
`;

const Author = styled.p`
  color: white;
  font-style: italic;
  font-size: 12px;
`;

const Image = styled.div`
  width: 150px;
  height: 200px;
  margin: 1em;
`;

class Container extends Component {
  constructor() {
    super();
    this.state = {};
  }
  getRandomName() {
    return names[Math.floor(Math.random() * (names.length - 1))];
  }
  render() {
    return (
      <ContainLeft
        style={{
          alignItems: 'center',
          position: 'absolute',
          width: '100%',
          margin: 0,
          marginTop: '5em',
          padding: 0,
          justifyContent: 'center'
        }}
      >
        <QuoteBox>
          <QuoteHolder>
            <Quote>"Lorem Ipsum Dolor Set Amet, Some other Words Quote"</Quote>
            <Author>{getRandomName}</Author>
          </QuoteHolder>
          <Image style={{ background: `url(${harry})` }} />
        </QuoteBox>

      </ContainLeft>
    );
  }
}
export default Container;

My child component holding the arrays I'll be using is: 我将使用的包含数组的子组件是:

const names = [
  {
    name: 'Yoda',
    id: 1
  },
  {
    name: 'Harry',
    id: 2
  },
  {
    name: 'Captain Kirk',
    id: 3
  },
  {
    name: 'Spock',
    id: 4
  },
  {
    name: 'Arnold',
    id: 5
  },
  {
    name: 'Gandalf',
    id: 6
  },
  {
    name: 'Magneto',
    id: 7
  },
  {
    name: 'Tony Stark',
    id: 8
  },
  {
    name: 'Bilbo',
    id: 9
  },
  {
    name: 'Obi Wan',
    id: 10
  }
];

const sayings = [
  {
    name: 'I find your lack of faith disturbing',
    id: 1
  },
  {
    name: 'Why, you stuck-up, half-witted, scruffy-looking nerf herder!',
    id: 2
  },
  {
    name: 'Do. Or do not. There is no try.',
    id: 3
  },
  {
    name: 'No. I am your father.',
    id: 4
  },
  {
    name: 'Now, young Skywalker, you will die.',
    id: 5
  },
  {
    name: 'I’m one with the Force. The Force is with me.',
    id: 6
  },
  {
    name: 'The force is strong with this one.',
    id: 7
  },
  {
    name: 'Use the force, Luke.',
    id: 8
  },
  {
    name: 'Judge me by my size, do you?',
    id: 9
  },
  {
    name: 'Fear is the path to the dark side.',
    id: 10
  }
];
export { names, sayings };

the error I get is: 我得到的错误是:

./src/components/pages/projectpages/quotes/container.js Line 64: 'getRandomName' is not defined no-undef

You have to bind it in your constructor 您必须将其绑定到构造函数中

constructor() {
    super();
    this.state = {};
    this.getRandomName = this.getRandomName.bind(this);
  }

Also you may import names array like this if you export it using default keyword, 另外,如果您使用default关键字将其导出,则可以这样导入名称数组,

import names from './arrays';

would solve the issue. 将解决问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM