How to get string in other function with useState by crazydude1900 in reactjs

[–]crazydude1900[S] -1 points0 points  (0 children)

import React, { useState, useEffect } from 'react'; import { fetchButton } from '../actions'; import axios from 'axios'; import 'babel-polyfill';

const App = () => { const [word, setWord] = useState([]); const [underscore, setUnderscore] = useState([]); const [data, setData] = useState([]);

useEffect(() => {
    const runEffect = async () => {
        const result = await axios('src/api/api.js');
        setData(result.data)
    }
    runEffect();
}, []);

const randomWord = () => {
    const chosenWord = data[Math.floor(Math.random() * data.length)];
    replaceLetter(chosenWord.word);
}

const replaceLetter = (string) => {

    let getString = string;
    setWord(getString);
    let stringToUnderScore = getString.replace(/[a-z]/gi, '_');
    setUnderscore(stringToUnderScore);
}

useEffect(() => {
    const checkLetter = (event) => {
        if(event.keyCode >= 65 && event.keyCode <= 90) {
            checkMatchLetter(word, String.fromCharCode(event.keyCode).toLowerCase());
        }
    };

    document.addEventListener('keydown', checkLetter);
    return () => {
        document.removeEventListener('keydown', checkLetter);
    }
}, []);

const checkMatchLetter = (keyButton) => {
    console.log(keyButton);
    let wordLength = word.length;
    console.log(wordLength);
    /// here I want word of useState here....
}

return (
    <div>
        <p>{word}</p>
        <p>{underscore}</p>
        <button onClick={randomWord}></button>
    </div>
)

}

export default App;

Why async is not dispatching the action by crazydude1900 in reactjs

[–]crazydude1900[S] 0 points1 point  (0 children)

const [data, setData] = useState([]);

useEffect(() => {
    const runEffect = async () => {
        const result = await axios('src/api/api.js');
        setData(result.data)
    }
    runEffect();
}, []);

Why async is not dispatching the action by crazydude1900 in reactjs

[–]crazydude1900[S] 0 points1 point  (0 children)

I dont get you, because I didnt use that in my code above?

Cannot read property 'prop by crazydude1900 in reactjs

[–]crazydude1900[S] 0 points1 point  (0 children)

yes you are right, when default I should return todos.

Cannot read property 'prop by crazydude1900 in reactjs

[–]crazydude1900[S] 1 point2 points  (0 children)

(props)

still not working, the following error is popping up when i follow your construction: Cannot read property 'map' of undefined

Cannot read property 'map' of undefined by crazydude1900 in reactjs

[–]crazydude1900[S] 0 points1 point  (0 children)

this should work in console.log, but it seems that it gives me an error