Home > Mobile >  how to store textarea new line value into array with react and send it to node js variable with post
how to store textarea new line value into array with react and send it to node js variable with post

Time:02-26

how to store textarea new line value into array with react and send it to node js variable with post request

const [inputvalue, setInputvalue] = useState("");

const  handleSubmit = (e) => {
  e.preventDefault();
  try {
    axios.post('/inputData', inputvalue , {
      headers: { 'Content-Type': 'text/plain' }
    })
      .then((response) => {
        setData(response.data);
      });
  } catch (e) {
    console.log('Error!', e);
  }
  }
<form onSubmit={handleSubmit}>
      
        <TextField id="outlined-multiline-static" label="Multiline" multiline rows={4} onChange={(e) => setInputvalue(e.target.value)} value={inputvalue} placeholder="Enter your keyword" />
      
      <button type="submit" className="btn btn-primary mt-3" onClick={() => setLoading(true)}>Click here</button>
    </form>

CodePudding user response:

I don't suggest keeping the inputvalue as an array (since it will mess with your textarea output). However, you can easily create the array when you submit it via axois.

Just use ".split(/\n/)" on your request to create the array of values.

You may want to do it like:

axios.post('/inputData', { values: inputvalue.split(/\n/) } , {
  headers: { 'Content-Type': 'text/plain' }
})
.then((response) => {
  setData(response.data);
});
  • Related