So I'm trying to make a simple add component in React native, and am currently struggling with various errors. I'm a beginner in React, and a lot of this seems like magic to me. Basically, i have a form which should take a value and add it to an array/list. Right now I'm doing this with the help of some tutorial but I'm getting errors. I tried this with setting const [test, setTest] = useState([1, 2, 3]), but this too had some errors. This is my code below, so please if you could help me. The current error is: A component is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. And displaying the array also is a problem for me in the render() part, so if you could please give me a heads up there too. Thanks!
import React, { useState } from 'react';
import { ListItem, Button } from 'react-native-elements'
export default class AddNote extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {
niz: ['test1', 'test2']
}
}
handleChange(event) { this.setState({value: event.target.value}); }
handleSubmit(event) {
event.preventDefault();
const {niz} = this.state;
const newItem = 'test3';
this.setState({
niz:[...this.state.niz, newItem]
})
console.log(niz);
}
render() {
return (
<div>
{/* {
niz.map(note => <ListItem
title={note.title}
bottomDivider
chevron
/>)
} */}
<form onSubmit={this.handleSubmit}>
<header> Add Note </header>
<input type="text" value={this.state.value} onChange={this.handleChange} />
<input type="submit" value="Add" />
</form>
</div>
);
}
}

