尝试传递参数时未定义未捕获的参考错误xyz

时间:2020-07-30 04:04:53

标签: javascript reactjs react-native

我目前正在尝试传递参数以设置当前大小的状态,具体取决于用户当前使用的选项,因此我设置了一个函数以及一个参数,无论何时触发onSelect,它都会传递大小,但是由于某种原因,我没有定义我的函数,我猜它是我的语法,但是我似乎无法弄清楚,这是我当前的代码:

Elasticsearch 6.x.x used to have type of document 
(where you could give type to your documents)


but Elasticsearch 7.x.x onwards it has no type or 
default type _doc, so you need to have _doc as your type 
while creating mapping.
const updateSize = userSize = () => {
      setSize(userSize);
      console.log(userSize);
    }

4 个答案:

答案 0 :(得分:1)

代替const updateSize = userSize = () => {使其const updateSize = (userSize) => {

答案 1 :(得分:1)

与其在单个选项上设置事件监听器,不如在<select>标记本身上进行设置。然后获取所选选项的文本,并将其用于游览代码中。

HTML

<select onchange="updateSize()" className="buttons form-control  mb-2" id="select">
     <option>Small</option>
     <option>Medium</option>
     <option>Large</option>
</select>

JS

<script>
     function updateSize() {
         let select = document.getElementById('select');
         let text = select.options[select.selectedIndex].text;
         console.log(text)
     }
</script>

根据选择的选项,文本值可以为“小”,“中”或“大”。

答案 2 :(得分:1)

问题是因为我的函数在另一个函数内部,而我没有注意到它不在范围之内。

答案 3 :(得分:0)

如果要将参数提供给const函数,请使用以下方法。

<select className="buttons form-control  mb-2">
     <option onSelect={()=>updateSize("Small")}>Small</option>
     <option onSelect={()=>updateSize("Medium")}>Medium</option>
     <option onSelect={()=>updateSize("Large")}>Large</option>
</select>

也用于updateSize函数。将参数保留在()中。

const updateSize = (userSize) => {
  setSize(userSize);
  console.log(userSize);
}