【问题标题】:How to get user input passed to params in axios get如何将用户输入传递给axios get中的参数
【发布时间】:2019-12-08 17:36:03
【问题描述】:

我正在尝试将用户从选择中选择的内容传递给我的 axios GET 中的参数。这是我所拥有的:

useEffect(() => {
    axios.get('/url-here/', {
        params: {
            value1: submitted
        }
    }).then(res => {
        console.log(res);
    }).catch(err => {
        console.error('err', err);
    });
}, []);

这是我的选择:

const [submitted, setSubmitted] = useState('');

<FormControl>
    <InputLabel id="submitted-select-label">Submitted</InputLabel>
    <Select
        labelId="submitted-select-label"
        id="submitted-select"
        value={submitted}
        onChange{e => setSubmitted(e.target.value)}
        input{<InputBase />}
    >
        <MenuItem value="submitted">Submitted</MenuItem>
        <MenuItem value="failed">Failed</MenuItem>
    </Select>
</FormControl>

【问题讨论】:

  • 您在这里遇到什么问题?
  • 参数在控制台中出现未定义。我不确定我错过了什么

标签: reactjs parameters axios parameter-passing


【解决方案1】:

一旦你定义了useEffect 并传递了函数,它的闭包基本上是陈旧的。 submitted 的值可能会在外部发生变化,但不会影响函数内部的值。

您需要使用第二个参数重新加载useEffect 中的值:

useEffect(() => {
    axios.get('/url-here/', {
        params: {
            value1: submitted
        }
    }).then(res => {
        console.log(res);
    }).catch(err => {
        console.error('err', err);
    });
}, [submitted]);

【讨论】:

    猜你喜欢
    • 2021-04-28
    • 2015-05-28
    • 1970-01-01
    • 2015-12-07
    • 2011-10-18
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多