【发布时间】:2020-12-05 19:18:12
【问题描述】:
代码:
import React, { useState } from 'react';
const lookup = {
"num": [
{ id: '1', text: '1' },
{ id: '2', text: '2' },
{ id: '3', text: '3' },
{ id: '4', text: '4' },
{ id: '5', text: '5' }
],
"en": [
{ id: 'a', text: 'a' },
{ id: 'b', text: 'b' },
{ id: 'c', text: 'c' },
{ id: 'd', text: 'd' },
{ id: 'e', text: 'e' }
]
}
const App = ({}) => {
this.setState()
const [ data ] = useState('num');
const { data } = this.state;
const options = lookup[data];
return (
<div>
<select onChange = {({ target: { value } }) => {this.setState({ data: value })}}>
<option value="num">Integers</option>
<option value="en">Alphabets</option>
</select>
<hr />
<select>
{options.map(a => <option key={a.id} value={a.id}>{a.text}</option>)}
</select>
</div>
);
}
export default App;
错误:
Failed to compile
./src/App.js
Line 30:11: Parsing error: Identifier 'data' has already been declared
28 | const [ data ] = useState('num');
29 |
> 30 | const { data } = this.state;
| ^
31 |
32 |
33 | const options = lookup[data];
从类组件更改为函数组件时出错。我英文不好我要级联select选项运行时出现编译错误我还是初学者,不知道怎么解决 我想要一个函数组件,而不是一个类组件
【问题讨论】:
标签: reactjs react-hooks cascadingdropdown