<html>
<head>
<meta charset="UTF-8" />
<script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
const { useState } = React
function App() {
const [fileContent, setFileContent] = useState('React Js Create/write text to file ');
const generateAndDownloadFile = () => {
const blob = new Blob([fileContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.txt');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
return (
<div className='container'>
<h2>React write/create text to file</h2>
<textarea
value={fileContent}
onChange={(e) => setFileContent(e.target.value)}
rows="5"
cols="40"
></textarea>
<button onClick={generateAndDownloadFile}>Generate File</button>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("app"));
</script>
<style>
.container {
width: 600px;
margin: 0 auto;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24);
padding: 20px;
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
h2 {
font-size: 24px;
margin-bottom: 10px;
}
textarea {
width: 300px;
height: 150px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
resize: vertical;
font-family: Arial, sans-serif;
font-size: 14px;
line-height: 1.5;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
font-family: Arial, sans-serif;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
</style>
</body>
</html>