I am new to react and am trying to adjust a feature on a small web app I am building to learn the ropes. Essentially, I am trying to split up a recipe's method on /n (new line) and store each element form the array as a split up object that will be viewed by the user as a series of steps.
Eg.
Put the pasta in the water
Heat the oil
Cook the sauce
Is supposed to be split into an array based on new lines, placed into objects and returned to the user as such:
Step1
Put the pasta in the water
Step 2
Heat the oil
Step 3
Cook the sauce
I am struggling with implementing this logic - I think I am putting it at the right spot. Any guidance would be very much appreciated.
const [methodStepsList, setMethodStepsList] = useState([])
const [methodStepObject, setMethodStepObject] = useState({
step_number: '',
step_instructions: '',
})
[...]
const handleMethodChange = (e) => { //captures the inputted text
let key = e.target.name
let value = e.target.value
console.log(`${key}:${value}`)
let prev = { ...methodStepObject }
prev[key] = value
setMethodStepObject(prev)
}
return (
<div>
<div className="recipe-form-container">
<form className="recipe-form">
[...]
{/* recipe method logic */}
<div className="recipe-blurb recipe-element">
<label>Recipe Method</label>
<span className="method-span">
<textarea
rows="5"
name="step_instructions"
type="text"
placeholder="Method will be split up based on new lines"
onChange={(e) => handleMethodChange(e)}
></textarea>
<button
onClick={(e) => {
setMethodStepList(()
=> [
e.split(/\r?\n/) //is supposed to split up the inputted text
])
e.preventDefault()
}}
>
Add Method
</button>
</span>
</div>