Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  53] [ 5]  / answers: 1 / hits: 10252  / 4 Years ago, sat, january 18, 2020, 12:00:00

I am adding Cards dynamically in my React functional component. Cards are stored in State. I map them and give id to each of them. OnClick on those Cards I get their id successfully. Now I want to getElementById to change Card color:



function Clicked(pressedGifId) {
if (pressedGifId === 'correctGif') CorrectMatch();
else WrongMatch();
}

function CorrectMatch(pressedGifId) {
// / THERE I GET Element: null
console.log('Element:', document.getElementById(pressedGifId));
}
function WrongMatch() {
console.log('wrong a match!');
}

export default function GameObject(props) {
const addedToGameGif = [];
const [pressedGifId, gifPressed] = useState(null);
const [photoCards, setPhotoCards] = useState([]);

useEffect(() => {
Clicked(pressedGifId);
}, [pressedGifId]);

// add randomly picked photos to addedToGameGif array
// ...

addedToGameGif.map(gifId =>
photoCards.push(
<Card id={gifId} onClick={() => gifPressed(gifId)}>
text
</Card>,
),
);

return <div>{photoCards}</div>;
}


I tried learning refs but they are only for class components. So how do I reach my element by id in React?


More From » reactjs

 Answers
-1

You can use ref in functional component as well. There is a hook called useRef.




Note: Never interact directly with DOM until or unless there is no api available in react to solve the problem for that particular use case.



In react it's not recommended to interact directly with dom. Always use react apis to interact with dom. React is designed to hide the DOM because they want to abstract the DOM away. By using the DOM directly you break the abstraction and make your code brittle to changes introduced in the library.



React is maintaining a virtual DOM if we make any changes in actual DOM directly then react will not be aware of this change and this can lead to some unexpected behavior .




import React, {useState, useRef} from 'react';

export default function GameObject(props) {
const addedToGameGif = [];
const [pressedGifId, gifPressed] = useState(null);
const [photoCards, setPhotoCards] = useState([]);
const elemRef = useRef(null);

useEffect(() => {
Clicked(pressedGifId);
}, [pressedGifId]);

// add randomly picked photos to addedToGameGif array
// ...

addedToGameGif.map(gifId =>
photoCards.push(
<Card ref={elemRef} id={gifId} onClick={() => gifPressed(gifId)}>
text
</Card>
)
);

return <div>{photoCards}</div>;
}



Example from official docs.



function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// `current` points to the mounted text input element
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type=text />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}

[#5017] Wednesday, January 15, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jadyngraysons

Total Points: 455
Total Questions: 109
Total Answers: 98

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
jadyngraysons questions
Thu, Apr 23, 20, 00:00, 4 Years ago
Tue, Dec 31, 19, 00:00, 4 Years ago
Wed, Dec 11, 19, 00:00, 5 Years ago
;