0

I am trying to import an array of words into my hangman game, however, I am encountering an error

Cannot read properties of undefined (reading 'split') Word /src/components/Word.js:8:20 5 | 6 | return ( 7 |

8 | {selectedWord.split('').map((letter, i) => { | ^ 9 | return ( 10 | 11 | {correctLetters.includes(letter) ? letter : ''}

Here is the Words.js file where the error appears to be occurring on line 8:

import React from 'react';

// rendering the list of correct words
const Word = ({ selectedWord, correctLetters }) => {

  return (
    <div className="word">
      {selectedWord.split('').map((letter, i) => {
        return (
          <span className="letter" key={i}>
            {correctLetters.includes(letter) ? letter : ''}
          </span>
        )
      })}
    </div>
  )
}

export default Word

Any help to fix this problem would be much appreciated, Thanks :)

2
  • 1
    Your hangmanarray export is a function, so your rename as words is still a function Commented Jan 3, 2022 at 13:00
  • you can return words from words.js and import as import {words} from "./components/words"; Commented Jan 3, 2022 at 13:00

1 Answer 1

2

You are exporting hangmanArray as a function that returns the words when executed. Either you should call it before using and store in a variable or export the words array directly.

const words = hangmanArray()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.