Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

split string in react and return as objects to the user

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>

>Solution :

  • Create a state for holding the value of the textarea: const [instructionsText, setInstructionsText] = useState("");
  • Set the value on change in textarea: <textarea ... onChange={(e) => setInstructionsText(e.target.value)} value={instructionsText} />
  • Create another state for your parsed array: const [instructionsArray, setInstructionsArray] = useState([]);
  • On your button click (it’s actually a form submit) prevent the default form submission behaviour and set the array state by reading your textarea state and splitting the string by newline characters: const handleSubmit = (e) => { e.preventDefault(); setInstructionsArray(instructionsText.split(/\r?\n/)); };

Full code:

https://codesandbox.io/s/small-night-tr7e1g

Actually, you don’t even need the second state, because you can directly split your string from textarea into array and render it like this:

{instructionsText.split(/\r?\n/).map((instruction, index) => {
    return <div>{`Step ${index + 1}: ${instruction}`}</div>;
})}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading