简体   繁体   English

我想在 react-native 中使用三元运算符我该如何修复我的代码?

[英]i want to use ternary operator in react-native how can i fix my code?

hi i want to use ternary operator嗨,我想使用三元运算符

if cookup has value i want to render如果烹饪有价值,我想呈现

   <FlatList
            data={cookUp}
            keyExtractor={(item) => String(item.id)}
            // keyExtractor={(item, index) => {
            //     return `${index}`;
            // }}
            renderItem={({item}) => (
            <Hey>
                {item.name}
            </Hey>
            )}
        />

but if cookup has a [] like this但是如果cookup有这样的[]

在此处输入图像描述

i want to render我想渲染

           <Hey>hiaaaaaa</Hey>

here is my code这是我的代码

  return (
    cookUp ?  ( 
        <FlatList
            data={cookUp}
            keyExtractor={(item) => String(item.id)}
            // keyExtractor={(item, index) => {
            //     return `${index}`;
            // }}
            renderItem={({item}) => (
            <Hey>
                {item.name}
            </Hey>
            )}
        />
    ) :
        (<Hey>hiaaaaaa</Hey>)
   );

how can i do this??我怎样才能做到这一点?? how can i fix my code?我该如何修复我的代码?

You can use the ListEmptyComponent prop in the flatlist您可以在 flatlist 中使用 ListEmptyComponent 道具

<FlatList
            data={cookUp}
            keyExtractor={(item) => String(item.id)}
            // keyExtractor={(item, index) => {
            //     return `${index}`;
            // }}
            renderItem={({item}) => (
            <Hey>
                {item.name}
            </Hey>
            )}
            ListEmptyComponent={<Hey>hiaaaaaa</Hey>}
        />

This will render the 'hiaaaaaa' when the data is empty.这将在数据为空时呈现“hiaaaaaa”。 No need to go through the ternary operator.无需通过三元运算符 go。

Try something like this:尝试这样的事情:

    return (
    { cookUp.length > 0 ?  ( 
        <FlatList
            data={cookUp}
            keyExtractor={(item) => String(item.id)}
            // keyExtractor={(item, index) => {
            //     return `${index}`;
            // }}
            renderItem={({item}) => (
            <Hey>
                {item.name}
            </Hey>
            )}
        />
    ) :
        (<Hey>hiaaaaaa</Hey>)
    }
  )

Don't forget to youse {} when you're writing JS in JSX.在 JSX 中编写 JS 时不要忘记使用 {}。 Other is just JavaScript that you should probably dive into a bit deeper before frameworks.其他只是 JavaScript,您可能应该在框架之前深入研究一下。

Should be pretty straightforward:应该很简单:

  return (
    <Fragment>
     {cookUp.length ?  ( 
        <FlatList
            data={cookUp}
            keyExtractor={(item) => String(item.id)}
            // keyExtractor={(item, index) => {
            //     return `${index}`;
            // }}
            renderItem={({item}) => (
            <Hey>
                {item.name}
            </Hey>
            )}
        />
    ) :
        (<Hey>hiaaaaaa</Hey>)
   }
   </Fragment>
   );

Try this approach,试试这个方法,

cookUp is an empty array, so you have to check the length for verifying the data is empty. cookUp是一个空数组,因此您必须检查长度以验证数据是否为空。

return (
<>
    {cookUp?.length ?  ( 
        <FlatList
            data={cookUp}
            keyExtractor={(item) => String(item.id)}
            // keyExtractor={(item, index) => {
            //     return `${index}`;
            // }}
            renderItem={({item}) => (
            <Hey>
                {item.name}
            </Hey>
            )}
        />
    ) :
        (<Hey>hiaaaaaa</Hey>) }
   </>
   );

An empty Array is true.Array为真。

After checking cookUp is true you can use the isArray method on cookUp :检查cookUptrue后,您可以在cookUp上使用isArray方法:

 return ( cookUp? Array.isArray(cookUp)? ( <FlatList data={cookUp} keyExtractor={(item) => String(item.id)} // keyExtractor={(item, index) => { // return `${index}`; // }} renderItem={({item}) => ( <Hey> {item.name} </Hey> )} /> ): ( < Hey > hiaaaaaa < /Hey>)); : null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM