I am building a simple React App to input: a title, a list of sentences (number depending on the user).
This is my App.js code:
import logo from './logo.svg';
import './App.css';
import {useState} from 'react';
// Import the functions you need from the SDKs you need
import { getFirestore, getDocs, collection, addDoc, deleteDoc, doc } from 'firebase/firestore'
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
// import {BrowserRouter as Router, Route, Switch} from 'react-router-dom';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
function App() {
let total = 0;
let [heading, setHead] = useState('');
let [anslst, setAnslst] = useState(['']);
let [swtch, setSwitch] = useState(true);
function Headingbar(){
return(
<div className="head-rect padd">
<div className="heading-div">
<div className="side-text">FILL</div>
<label className="switch">
<input className="inputbox" type="checkbox" checked={!swtch} onChange={(e) => { setSwitch(!e.target.checked); }}/>
<span className="slider round"></span>
</label>
<div className="side-text">VIEW</div>
</div>
</div>
)
}
function Fill() {
return (
<>
<form className="padd title-form">
<input type="text" className="title-textbox textbox-defaults" placeholder="Title" onChange={(e) => { setHead(e.target.value) }} />
</form>
{
anslst.map((i)=>{
total += 1;
return(
<form className="padd answer-form">
<textarea rows="10" cols="60" type="text" id={total - 1} className="answer-textbox textbox-defaults" placeholder="points" onChange={(e) => {
anslst[e.target.id] = e.target.value;
setAnslst(anslst);
}} />
</form>
);
})
}
<div className="bottom padd">
<form>
<input type="button" value="+" className="padd plus-button" onClick={()=>{
anslst = [...anslst, ''];
setAnslst(anslst);
console.log(anslst);
}}/>
</form>
<form>
<input type="button" value="Submit" onClick={()=>{add(heading, anslst)}}/>
</form>
</div>
</>
)
}
return (
<div className="App">
<Headingbar />
{swtch && <Fill />}
</div>
);
}
export default App;
The main piece of code in focus is:
<form className="padd title-form">
<input type="text" className="title-textbox textbox-defaults" placeholder="Title" onChange={(e) => { setHead(e.target.value) }} />
</form>
This form seems to malfunction ie on entering values into it , the cursor automatically disappears from the form. Sometimes randomly one of the letters is filled into the form before the cursor disappears.
I expected the form to take any input I give to it.
My debugging attempts:
- When I remove the 'onChange' part of the input tag, it works as expected.
- When I replace the function inside onChange with any function which doesn't use setHead function, the form works as expected.
This convinced me that the problem is with setHead() but I couldn't understand what.