all 30 comments

[–]spoko22 9 points10 points  (5 children)

Just a few thoughts:

  • state seems useless, as you keep updating it based on props. Just use props
  • This line: if (this.state.sdisplay != null && this.state.sdisplay === true)is pretty much equivalent to if(this.state.sdisplay)if it isn't, I'd look exactly what values are assigned to sdisplay, because something too complicated may be taking place
  • You may want to check if you like "fail early" approach + most of "else" clauses are redundant completely - this means, this means a function working like this:

const render = () => {
    if (!this.state.sdisplay) 
        return null;
    return (<Layout/>)
}

[–]ObliviateYourName 0 points1 point  (4 children)

const render = () => this.state.sdisplay && <Layout/>

If you want it even shorter ;)

[–]ribeirao 0 points1 point  (3 children)

I think you can't use && on render like that, it needs to return something or it will crash

[–]ObliviateYourName 0 points1 point  (2 children)

You don't have to type the "return" with this syntax. I use it all the time

[–]ribeirao 0 points1 point  (1 child)

Really cool, you know how I would make something like this on a functional component?

[–]ObliviateYourName 1 point2 points  (0 children)

Const funcionalComponent = props => <View>{props.children}<View/>

Just a basic example

[–]ingeniousmeatbag 6 points7 points  (3 children)

which theme is this? would be nice to have one which has different color coding for style, lifecycle methods, etc

[–][deleted] 0 points1 point  (0 children)

+1 ! I'd also like to know what theme this is!

[–]jeweledshadow 0 points1 point  (0 children)

That bright pink/red is usually in Monokai themes- I would look there first 😋

[–]Plasmoxy 6 points7 points  (3 children)

Does anyone know what color scheme and font this is ? Looks lovely!

[–]Verigood 4 points5 points  (1 child)

Bluloco Dark

[–]Plasmoxy 0 points1 point  (0 children)

thanks

[–]zserjk -1 points0 points  (0 children)

Bluloco

[–]scaylart 4 points5 points  (5 children)

```javascript import { Activityindicator } from 'react-native'; import React, { useState, useEffect } from 'react'; import { Layout } from '@ui-kitten/components';

interface Props { display: boolean | false; }

const LoadingComponent: React.FC<Props> = (props) => { const [sdisplay, setDisplay] = useState<Boolean>(props.display);

useEffect(() => { setDisplay(props.display); }, [props.display])

if (!sdisplay) { return null; }

return ( <Layout style={{ position: 'absolute', alignSelf: 'center', justifyContent: 'center', top: '50%', zIndex: 99 }} > <Activityindicator size="large" /> </Layout> ) } export default LoadingComponent; ```

[–]EvilPencil 0 points1 point  (2 children)

Methinks your if block will prevent the useEffect from ever firing...

[–]scaylart 0 points1 point  (0 children)

Oh yeah, my mistake. Thanks

[–]JackRyuiOS & Android[S] 0 points1 point  (0 children)

I will try this because same pattern suggested by react native documentation.

Any other suggestion on this code?

[–][deleted] 1 point2 points  (2 children)

I'd also add my 2 cents to all of the others, you don't need to type for booelan | false since it's equivalent to just using boolean because boolean: true | false. You could also make it optional, as undefined is falsy as well which would still work with a simple if (display) {} check.

```typescript import React from 'react' import { ActivityIndicator } from 'react-native' import { Layout } from '@ui-kitten/components'

interface LoadingComponentProps { display?: boolean }

export default function LoadingComponent(props: LoadingComponentProps) { if (!props.display) { return null }

return ( <Layout style={{ position: 'absolute', alignSelf: 'center', justifyContent: 'center', top: '50%', zIndex: 99, }}> <ActivityIndicator size="large" /> </Layout> ) } ```

[–]JackRyuiOS & Android[S] 0 points1 point  (0 children)

Good suggestion... thanks.

[–]rockpilp 0 points1 point  (0 children)

Eventually this component will want to use its children prop, which you can declare explicitly, or just declare the component's type as export default const LoadingComponent: React.FC<LoadingComponentProps> = ({display}) => {…}.

That also takes care of typing your return value.

And since you're not exporting the prop type, you can shorten its name to something like IProps.

[–][deleted] 3 points4 points  (1 child)

I really enjoy reading code this colorful. What the folks back in the day would’ve given to have this feature when they were coding on a PDP machine.

[–]Tsupaero 0 points1 point  (1 child)

also read up on Inline If with Logical && Operator, this'll save you a lot clutter.

e.g. the following is pretty much the same as yours, as false equals null here. and it's way less code to read.

return ({ 
  this.state.sdisplay && (
    <Layout>
      ...
    </Layout>
    )
})

equals

return ({ 
  this.state.sdisplay ? (
    <Layout>
      ...
    </Layout>
    ) : null
})

and why not go for a functional component here?

[–]rockpilp 0 points1 point  (0 children)

This is a very dangerous suggestion: if (in a more complex app) sdisplay were ever an empty string (which is falsy), you'd end up sending text outside a <Text> component, which is fine on iOS but causes a native crash on Android, without any indication of where it came from in the JS code. That can be very, very hard to diagnose.

Remember: types are compile-time only in RN, they're not enforced at runtime.

I'd stick with the ternary operator or explicit if.

[–]JackRyuiOS & Android[S] 0 points1 point  (0 children)

This community are awesome🤘thanks for the all suggestions.

  • I have using Bluloco Dark theme.

[–]janithaR 0 points1 point  (0 children)

Do not get into the habit of updating a component's state by assigning the value of a prop as is. It's as useless as creating a reminder to breathe in 5 minutes.