Mam kod który działa ale nie jest zgodny z dobrą praktyką, zmienia stan bezpośrednio, jak to zrobić lepiej?
import React, { useState } from 'react'
import { connect } from 'react-redux'
import DatePicker from "react-datepicker"
import "react-datepicker/dist/react-datepicker.css";
function Add(props) {
console.log(props);
const [startDate, setStartDate] = useState(null);
const [newTask, setNewTask] = useState({
title: null,
titleMarked: false,
id: null,
date: null,
detail: [
{ point: null, pointMarked: false },
{ point: null, pointMarked: false },
{ point: null, pointMarked: false }
]
})
const handleChange = (e) => {
if (e.target.id === 'newTask') {
newTask.title = e.target.value
} if (e.target.id === 'pointOne') {
newTask.detail[0] = { point: e.target.value, pointMarked: false }
} if (e.target.id === 'pointTwo') {
newTask.detail[1] = { point: e.target.value, pointMarked: false }
} if (e.target.id === 'pointThree') {
newTask.detail[2] = { point: e.target.value, pointMarked: false }
}
newTask.id = new Date().getTime()
}
const handleSubmit = (e, id) => {
e.preventDefault()
newTask.date = startDate
props.addNewTask(newTask)
props.displayDetail(newTask.id)
props.display('task')
console.log(startDate);
}
const handleDate = (date) => {
setStartDate(date)
}
return (
<>
<form onSubmit={handleSubmit} type='submit'>
<input onChange={handleChange} id='newTask' type="text" />
<label htmlFor="text">new task</label>
<input onChange={handleChange} placeholder='optional' id='pointOne' type="text" />
<label htmlFor="text">point one</label>
<input onChange={handleChange} placeholder='optional' id='pointTwo' type="text" />
<label htmlFor="text">point two</label>
<input onChange={handleChange} placeholder='optional' id='pointThree' type="text" />
<label htmlFor="text">point three</label>
<DatePicker
selected={startDate}
onChange={date => handleDate(date)}
timeInputLabel="Time:"
dateFormat="MM/dd/yyyy h:mm aa"
showTimeInput
placeholderText="Click to select a date"
/>
<button>ADD</button>
</form >
</>
)
}
const mapStateToProps = (addNewTask) => {
return {
add: addNewTask
}
}
const mapDispatchToProps = (dispatch) => {
return {
addNewTask: (newTask) => { dispatch({ type: 'ADD_NEW_TASK', newTask: newTask }) },
displayDetail: (id) => { dispatch({ type: 'DISPLAY_DETAIL', id: id }) },
display: (id) => { dispatch({ type: 'DISPLAY', id: id }) }
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Add)
chodzi o metodę handleChange, ktoś wie jak to zrobić lepiej? problem w tym że jest tam tablica
proszę o radę