1
I’m creating a kind of Todolist in React and had the idea to use two inputs.
The code is divided into 3 components:
App.js:
import React, { useState } from "react";
import Form from "./Form";
import Modal from './Modal'
const App = () => {
const [value, setValue] = useState("");
const [value2,setValue2] = useState("")
const [todos, setTodos] = useState([]);
const getValue = (event)=>{
setValue(event.target.value)
}
const getValue2=(event)=>{
setValue2(event.target.value)
}
return (
<div>
<Form
valueProp={value}
valueProp2={value2}
onChangeProp={getValue}
onChangeProp2={getValue2}
typeProp="submit"
handleSubmitProp={(e)=>{
e.preventDefault();
setTodos([...todos, value]);
}}/>
<ul>
{todos.map((todo, index) => (
<li key={index}><Modal title={todo} content={value2}/></li>
))}
</ul>
</div>
);
};
export default App;
Form.js:
import React from 'react'
export default function Form({valueProp,valueProp2,typeProp,onChangeProp,onChangeProp2,handleSubmitProp}){
return(
<>
<form onSubmit = {handleSubmitProp}>
<input value = {valueProp} onChange={onChangeProp}/>
<input value = {valueProp2} onChange={onChangeProp2}/>
<button type={typeProp}>ENTER</button>
</form>
</>
)
}
Modal.js:
import React from 'react'
export default function Modal({title,content}){
return(
<>
<p>{title}</p>
<p>{content}</p>
</>
)
}
The problem with this application is that when the items (placed in the input) appear on the screen the following happens:
The estate
title
when rendered is unchangeable after Submit (which is what I want).Already the property
content
, when rendered is not immutable, because when the input is moved again it changes, following the value of the second input.
To make it clearer I’ll put an image:
This is the situation of the screen after I give the Ubmit, apparently normal.
This is the screen situation after I change the input value (without giving Ubmit again !), notice that the first value was unchanged but the second changed.
I believe the error is in the mapping part (line 30 of the App.js
). Can someone help me solve this problem ? what I need to do to make the second value of the input unchanged after rendering ?
Thank you so much ! Your solution has helped too much !
– Lucca