Answer the question
In order to leave comments, you need to log in
How to call a component in a function?
There is a Hi component
import React from 'react';
const Hi = () => {
return (
<div>
<h1>hi</h1>
</div>
);
};
export default Hi;
import React from 'react';
import Hi from './Hi'
function display(){
return <Hi/>
}
const Main = () => {
return (
<div>
<button onClick={display}>отобразить</button>
</div>
);
};
export default Main;
Answer the question
In order to leave comments, you need to log in
I think you need to learn more about React and its basic concepts. Especially, to understand how to use the state of the component. Sometimes the documentation seems very dry, so Youtube is here to help)
import React, { useState } from 'react';
import Hi from './Hi'
function display(){
return <Hi/>
}
export const Main = () => {
const [isHiVisible, setHiVisible] = useState(false);
const onClick = () => {
setHiVisible((prev) => !prev);
}
return (
<div>
<button onClick={onClick}>отобразить</button>
{isHiVisible &&
<Hi/>
}
</div>
);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question