while I am creating new “Todo” and modifying the fields are no problem my issue is receiving back the child data when I click the “show all objects” button.
Here is a code example to illustrate my issue:
JavaScript
x
91
91
1
const CompoOne = () =>{
2
const [todos, setTodos] = useState([])
3
const [subject, setSubject] = useState("")
4
const [priority, setPriority] = useState("")
5
6
const addTodo = () => setTodos(
7
[todos, { a: "Title", b: "subTitle", c: "content"}]
8
)
9
10
return (
11
<>
12
<InputText
13
value={subject}
14
setValue={setSubject}
15
label="subject field"
16
/>
17
<InputText
18
value={priority}
19
setValue={setPriority}
20
label="priority field"
21
/>
22
<hr />
23
<h2>Todo:</h2>
24
<hr />
25
<button
26
onClick={()=>addTodo()}
27
>
28
add a todo
29
</button>
30
<>
31
{setTodos.map((el,i)=>(
32
<CompoTwo
33
todo={el}
34
setTodos={setTodos}
35
todos={todos}
36
key={i}
37
/>
38
)}
39
</>
40
41
<button
42
onClick={()=>console.log(todos)}
43
>
44
show all objects
45
</button>
46
</>
47
)
48
}
49
50
51
const compoTwo = ({ todo, todos, setTodos }) =>{
52
const [a, setA] = useState("")
53
const [b, setB] = useState("")
54
const [c, setC] = useState("")
55
56
const handleChange = (e) =>{
57
const {name, value} = e.target
58
setTodos((prevState)=>{
59
//I would like to update the inside the prevState instance
60
const updatedArray = // something like [...prevState, [name]:value]
61
// return updatedArray
62
})
63
}
64
65
return(
66
<>
67
<InputText
68
value={a}
69
name="a"
70
onChange={(e)=>setA(e.target.value)}
71
onBlur={(e) => handleChange(e)}
72
label="field A"
73
/>
74
<InputText
75
value={b}
76
name="b"
77
onChange={(e)=>setB(e.target.value)}
78
onBlur={(e) => handleChange(e)}
79
label="field B"
80
/>
81
<InputText
82
value={c}
83
name="c"
84
onChange={(e)=>setC(e.target.value)}
85
onBlur={(e) => handleChange(e)}
86
label="field C"
87
/>
88
</>
89
)
90
}
91
if I click twice on the “add a Todo” button and change one of the field’s values, I will still get the default value
JavaScript
1
5
1
[
2
{a: "Title", b: "subTitle", c:"content"},
3
{a: "Title", b: "subTitle", c:"content"},
4
]
5
and my purpose would be to get the updated value of the child component.
if you got any idea…
here is the code sandbox link to try and play
thanks in advance
Advertisement
Answer
You can try this and hope it may help you for compoTwo.
Note: You can also set the todo prop value as the initial value of useState.
JavaScript
1
47
47
1
const compoTwo = ({ todo, todos, setTodos }) =>{
2
const [a, setA] = useState("")
3
const [b, setB] = useState("")
4
const [c, setC] = useState("")
5
6
const handleChange = (e) => {
7
const { name, value } = e.target;
8
setTodos((prevState) => {
9
const temp = [prevState];
10
const find = temp.find((res) => res.id === todo.id);
11
if (find) {
12
find[name] = value;
13
return temp;
14
} else {
15
return prevState;
16
}
17
});
18
};
19
20
return(
21
<>
22
<InputText
23
value={a}
24
name="a"
25
onChange={(e)=>setA(e.target.value)}
26
onBlur={(e) => handleChange(e)}
27
label="field A"
28
/>
29
<InputText
30
value={b}
31
name="b"
32
onChange={(e)=>setB(e.target.value)}
33
onBlur={(e) => handleChange(e)}
34
label="field B"
35
/>
36
<InputText
37
value={c}
38
name="c"
39
onChange={(e)=>setC(e.target.value)}
40
onBlur={(e) => handleChange(e)}
41
label="field C"
42
/>
43
</>
44
)
45
}
46
47