javascript - 使用 Switch 语句在 React 中预期的表达式

标签 javascript reactjs react-native

我在 react 文件中使用 switch 语句。在第一个案例行中获取表达式预期错误。

export default ({handle, state}) => (
  <div style={styles.container} >
    <h3 style={{margin: 0, marginBottom: 15}}>InputData</h3>
    {items.map((item) => (
      <div style={styles.lineContainer}>
        switch(item.name){
          case "name1": return <InputBox/>;
          case "name2": return <SelectBox/>;
          case "name3": return <<SelectBox/>;/>;
          default: return <InputBox/>
        }
      </div>
    ))}
  </div>
);

最佳答案

如果要内联 switch 语句,则需要将其封装在 IIFE 中。

export default ({handle, state}) => (
  <div style={styles.container}>
    <h3 style={{margin: 0, marginBottom: 15}}>InputData</h3>
    {items.map((item) => (
      <div style={styles.lineContainer}>
        {(() => {
          switch(item.name) {
            case "name1": return <InputBox/>;
            case "name2": return <SelectBox/>;
            case "name3": return <SelectBox/>;
            default: return <InputBox/>
          }
        })()}
      </div>
    ))}
  </div>
);

关于javascript - 使用 Switch 语句在 React 中预期的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55237619/

相关文章:

javascript - 在 Javascript 对象中创建新对象的最佳实践

javascript - 每当可选 Prop 发送到子组件时显示错误

javascript - 如何使用react-native-vision-camera代替expo-camera?

javascript - React Native Android 模块不起作用

javascript - 博览会- native react : there is way to see the content of the expo FileSystem and also delete content from there?

Javascript 打字机无法正确写入和退格

javascript - 如何在 api 调用中添加 header ?

javascript - 无法访问 .beforeEach() 中的 Buefy 组件

reactjs - enzyme shallowWrapper.setState 不适用于redux 连接组件

javascript - 为什么 include() 在我的代码中不能正常工作?