简体   繁体   English

如何使用 react 将 textarea 新行值存储到数组中,并使用 post 请求将其发送到 node.js 变量

[英]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如何使用 react 将 textarea 新行值存储到数组中,并使用 post 请求将其发送到 node.js 变量

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.但是,您可以在通过 axois 提交时轻松创建数组。

Just use ".split(/\n/)" on your request to create the array of values.只需根据您的请求使用“.split(/\n/)”来创建值数组。

You may want to do it like:你可能想这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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