简体   繁体   中英

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

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>

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM