【问题标题】:Is there a way to use a map inside of another map function?有没有办法在另一个地图功能中使用地图?
【发布时间】:2022-01-07 07:29:58
【问题描述】:

我想要做的是在字段名称之后,我想呈现它的类型,如你所见,它处于另一种状态:

我的代码:

    const FormScreen = ({route}) => {
      const [FieldForm, setFieldForm] = useState([]);
      const [TypeForm, setTypeForm] = useState([]);
    
    useEffect(() => {
      if (userForm.length > 0) {
        return;
      } else {
        setFieldForm(JSON.parse(route.params.paramKey).fields);
        setTypeForm(JSON.parse(route.params.paramKey).type);
        console.log(FieldForm,TypeForm);  // returns me: {"message":["ab","test"],"tipo":["Date","Text"]}
      }
    },[userForm,TypeForm]);   
    return (
      <SafeAreaView style={{flex: 1}}>
        <View>
          <Text>COLLECTION :</Text>
    
          {FieldForm.length > 0 ? (
            FieldForm.map((item) => (          
              <Text key={uuid.v4()}>{item}</Text>
              //Here I want to to map which type is the field(to use conditionals to render) but how can I make it? 
            ))
          ) : (
            <Text key={uuid.v4()}> Loading ...</Text>
          )}
    
        </View>
      </SafeAreaView>
    );
    };

如何使用地图内部的地图来执行此操作,或创建 api 调用以返回字段的类型?

【问题讨论】:

  • 您可以在另一个地图中进行映射。当你尝试它时发生了什么?
  • @evolutionxbox 我无法在不生成 sintax 错误的情况下将类型映射插入到字段映射中

标签: javascript react-native jsx


【解决方案1】:

您绝对可以嵌套map 函数:

const FormScreen = ({route}) => {
      const [FieldForm, setFieldForm] = useState([]);
      const [TypeForm, setTypeForm] = useState([]);
    
    useEffect(() => {
      if (userForm.length > 0) {
        return;
      } else {
        setFieldForm(JSON.parse(route.params.paramKey).fields);
        setTypeForm(JSON.parse(route.params.paramKey).type);
        console.log(FieldForm,TypeForm);  // returns me: {"message":["ab","test"],"tipo":["Date","Text"]}
      }
    },[userForm,TypeForm]);   
    return (
      <SafeAreaView style={{flex: 1}}>
        <View>
          <Text>COLLECTION :</Text>
    
          {FieldForm.length > 0 ? (
            FieldForm.map((item) => (
              <>          
                <Text key={uuid.v4()}>{item}</Text>
                <> {TypeForm.length && TypeForm.map((type) => ( // Return node )) || null};
                </>
              </>
            ))
          ) : (
            <Text key={uuid.v4()}> Loading ...</Text>
          )}
    
        </View>
      </SafeAreaView>
    );
    };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 2016-06-15
    相关资源
    最近更新 更多