3

I have a simple form with an input field that I can't type on. I first thought the problem was with the onChange or the value props that were setting the input to readonly, but the fact is that I cant type with the browser suggestions and the state updates perfectly (See gif here) it's just that I won't let me type with the keyboard, even after reloading the page.

I also have a Login page that works perfectly except when I log out and redirect back to that page, it won't work until I reload the page, now it will work.

<input
value={name}
onChange={handleChange}
name="name"
/>
const [name, setName] = useState("");

const handleChange = (e:any) => {
    setName(e.target.value);
}

Weird thing is that it's in like a readonly state but when I use browser suggestions it works and updates the state.

Here is the whole component:

import React, { useEffect, useState } from 'react';
import { useForm } from '../../utils/useForm';

import { CubeType } from '../../interfaces';

//import useStore from '../store/Store';

import { Modal, Button, Row, Col, FormGroup, FormLabel, FormControl } from 'react-bootstrap';

type Props = {
    show: Boolean,
    onClose: () => void,
    cubeTypes: CubeType[]
};

const ModalTimelist = (props: Props) => {
    //const store = useStore();
    const [values, handleChangee] = useForm({ cubeType: 1, name: '' });


    const [name, setName] = useState("");

    const handleChange = (e:any) => {
        setName(e.target.value);
    }

    useEffect(() => {
        const modal = document.getElementsByClassName('modal')[0];
        if(modal) modal.removeAttribute('tabindex');
    }, [props.show]);

    return (
        <>
            <Modal show={props.show} onHide={ props.onClose }>
                <Modal.Header>
                    <Modal.Title>Timelist { name }</Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    <Row>
                        <Col md="3">
                            <FormGroup>
                                <FormLabel>Cube Type</FormLabel>
                                <select
                                    value={values.cubeType}
                                    onChange={ handleChangee }
                                    className="form-select"
                                    name="cubeType"
                                >
                                    {props.cubeTypes.map((it, idx) => {
                                        return (<option value={ idx } key={"cube"+idx}>{it.name}</option>);
                                    }) }
                                </select>
                            </FormGroup>
                        </Col>
                        <Col md="9">
                            <FormGroup>
                                <FormLabel>Name</FormLabel>
                                <FormControl
                                    value={name}
                                    onChange={handleChange}
                                    name="name"
                                />
                            </FormGroup>
                        </Col>
                    </Row>
                </Modal.Body>
                <Modal.Footer>
                    <Button variant="success" onClick={() => props.onClose()}>
                        Save
                    </Button>
                    <Button variant="outline-danger" onClick={() => props.onClose()}>
                        Cancel
                    </Button>
                </Modal.Footer>
            </Modal>
        </>
    );
}

export default ModalTimelist;
Roger Esteve
  • 31
  • 1
  • 3

4 Answers4

2

value of input must be the state value otherwise it will not change use this code

const App = () => {
   const [name,setName] = useState("")
   const handle = ({target:{value}}) => setName(value)
   return <input
     value={name}
     onChange={handle}
      name="name"
    />
}
Omar Khaled
  • 439
  • 1
  • 4
  • 16
2

Use a debounce for setting name on state.

Example:

const handleChange = (e:any) => {
    debounce(() => { setName(e.target.value) }, 300);
}
Suneel K
  • 84
  • 1
  • 9
1

Names specified by you in input field attributes must be same as useState names. Otherwise this problem occurs.

Example:

<input type={"text"} className="form-control" placeholder='Enter your User Name' name="username" value={username} onChange={(e)=>onInputChange(e)}/>

In name="username" , username spell must be same as the spell you used in State.

0

I tried the code and it works fine I think you should change the browser

and if you want

change this

const ModalTimelist = (props: Props) => {

with

const ModalTimelist:React.FC<Props> = (props) => {
Omar Khaled
  • 439
  • 1
  • 4
  • 16