How to render Alert Component conditionally in react native The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceHide keyboard in react-nativeWhat is the difference between using constructor vs getInitialState in React / React Native?How to conditionally add attributes to React components?What is the difference between React Native and React?React-redux tutorial : Where does children come fromTypescript error on rendered component when using react-redux connect functionRedux - mapDispatchToProps - TypeError: _this.props.setCurrentUserHandle is not a functionmapStateToProps() in Connect(EquipmentMetadata) must return a plain object. Instead received undefinedNot able to navigate to other page in react nativeValidate multiple fields from frontend using react native

Windows 10: How to Lock (not sleep) laptop on lid close?

Deal with toxic manager when you can't quit

What can I do if neighbor is blocking my solar panels intentionally?

Word to describe a time interval

Mortgage adviser recommends a longer term than necessary combined with overpayments

One-dimensional Japanese puzzle

How to handle characters who are more educated than the author?

How to read αἱμύλιος or when to aspirate

How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time

How do spell lists change if the party levels up without taking a long rest?

Sub-subscripts in strings cause different spacings than subscripts

Is an up-to-date browser secure on an out-of-date OS?

Accepted by European university, rejected by all American ones I applied to? Possible reasons?

Keeping a retro style to sci-fi spaceships?

60's-70's movie: home appliances revolting against the owners

How to make Illustrator type tool selection automatically adapt with text length

The following signatures were invalid: EXPKEYSIG 1397BC53640DB551

How to determine omitted units in a publication

How many cones with angle theta can I pack into the unit sphere?

What aspect of planet Earth must be changed to prevent the industrial revolution?

Why are PDP-7-style microprogrammed instructions out of vogue?

Is there a way to generate uniformly distributed points on a sphere from a fixed amount of random real numbers per point?

Is this wall load bearing? Blueprints and photos attached

Using dividends to reduce short term capital gains?



How to render Alert Component conditionally in react native



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceHide keyboard in react-nativeWhat is the difference between using constructor vs getInitialState in React / React Native?How to conditionally add attributes to React components?What is the difference between React Native and React?React-redux tutorial : Where does children come fromTypescript error on rendered component when using react-redux connect functionRedux - mapDispatchToProps - TypeError: _this.props.setCurrentUserHandle is not a functionmapStateToProps() in Connect(EquipmentMetadata) must return a plain object. Instead received undefinedNot able to navigate to other page in react nativeValidate multiple fields from frontend using react native



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am trying to customize the Alert Component. For that in the render() method I am
wrapping Alert inside the AlerBoxContainer class (that is placed in alertbox-container.tsx file) . I am rendering it by toggling using a prop.



Approach -1 : By directly returning the Alert component if the condition is true or returning null if condition is false.



This is in alertbox-container.tsx file:



import * as React from "react";
import Alert from "react-native";
import connect from "react-redux";
import Dispatch from "redux";
import State from "../../../../store/reducer";
import * as constants from "../../constants";
import * as selectors from "../../selectors";
import AlertBox, AlertBoxContainerProps from "./alertbox";

interface AlertBoxState
showAlert: boolean;
blurred: boolean;
appState: any;


class AlertBoxContainer extends React.Component<AlertBoxContainerProps, AlertBoxState>
constructor(props)
super(props);


render()
return this.props.isAlertVisible ?
Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
text: constants.CANCEL_TEXT, onPress: () => this.props.onCancel() ,
text: constants.EXIT_TEXT, onPress: () => this.props.onExit()
])
: null;



const mapStateToProps = (state: State): AlertBoxContainerProps => (
isAlertVisible: selectors.getIsAlertVisible(state)
);

const mapDispatchToProps = (dispatch: Dispatch<core.features.workplace.Action>): AlertBoxContainerProps => (
toggleAlertVisible: () => dispatch(core.features.workplace.toggleAlertVisible())
);

export default connect<any, AlertBoxContainerProps, AlertBoxContainerProps>(
mapStateToProps,
mapDispatchToProps
)(AlertBoxContainer);


When I tried the above code I got the error :



Property 'render' in type 'AlertBoxContainer' is not assignable to the same property in base type 'Component'.
Type '() => void' is not assignable to type '() => ReactNode'.
Type 'void' is not assignable to type 'ReactNode'.ts(2416)



I also tried in the following way :



Aprroach - 2:



in alertbox-container.tsx :



 render() 
return (
<AlertBox
isAlertVisible=this.props.isAlertVisible
onCancel=this.props.onCancel
onExit=this.props.onExit
/>
);



In alertbox.tsx file :



import Alert, View from "react-native";
import * as constants from "../../constants";
export interface AlertBoxContainerProps
isAlertVisible?: boolean;
toggleAlertVisible?: () => any;
navigation?: any;
hardwareBackPress?: () => any;
onExit?: () => any;
onCancel?: () => any;


export const AlertBox = (props: AlertBoxContainerProps) =>
return props.isAlertVisible
? (
Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
text: constants.EXIT_TEXT, onPress: () => props.onExit()
])
)
: null;
;


When I tried this code I got the error : " JSX element type 'void' is not a constructor function for JSX elements.ts(2605) "



How can I get rid of these errors and render the alert box?



When I tried with the second approach and by modifying the code I am able to render the Alert, with the following code :



Approach -3 :



In alertbox.tsx :



export const AlertBox = (props: AlertBoxContainerProps) => 
return (
<View>

props.isAlertVisible
? Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
text: constants.EXIT_TEXT, onPress: () => props.onExit()
])
: null
</View>
);


;



But the code is with the weird syntax where in the view I need to place empty ' ' other wise I get the error :



" Type 'void' is not assignable to type 'ReactNode'.ts(2322)
index.d.ts(725, 39): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly>' " .










share|improve this question






























    0















    I am trying to customize the Alert Component. For that in the render() method I am
    wrapping Alert inside the AlerBoxContainer class (that is placed in alertbox-container.tsx file) . I am rendering it by toggling using a prop.



    Approach -1 : By directly returning the Alert component if the condition is true or returning null if condition is false.



    This is in alertbox-container.tsx file:



    import * as React from "react";
    import Alert from "react-native";
    import connect from "react-redux";
    import Dispatch from "redux";
    import State from "../../../../store/reducer";
    import * as constants from "../../constants";
    import * as selectors from "../../selectors";
    import AlertBox, AlertBoxContainerProps from "./alertbox";

    interface AlertBoxState
    showAlert: boolean;
    blurred: boolean;
    appState: any;


    class AlertBoxContainer extends React.Component<AlertBoxContainerProps, AlertBoxState>
    constructor(props)
    super(props);


    render()
    return this.props.isAlertVisible ?
    Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
    text: constants.CANCEL_TEXT, onPress: () => this.props.onCancel() ,
    text: constants.EXIT_TEXT, onPress: () => this.props.onExit()
    ])
    : null;



    const mapStateToProps = (state: State): AlertBoxContainerProps => (
    isAlertVisible: selectors.getIsAlertVisible(state)
    );

    const mapDispatchToProps = (dispatch: Dispatch<core.features.workplace.Action>): AlertBoxContainerProps => (
    toggleAlertVisible: () => dispatch(core.features.workplace.toggleAlertVisible())
    );

    export default connect<any, AlertBoxContainerProps, AlertBoxContainerProps>(
    mapStateToProps,
    mapDispatchToProps
    )(AlertBoxContainer);


    When I tried the above code I got the error :



    Property 'render' in type 'AlertBoxContainer' is not assignable to the same property in base type 'Component'.
    Type '() => void' is not assignable to type '() => ReactNode'.
    Type 'void' is not assignable to type 'ReactNode'.ts(2416)



    I also tried in the following way :



    Aprroach - 2:



    in alertbox-container.tsx :



     render() 
    return (
    <AlertBox
    isAlertVisible=this.props.isAlertVisible
    onCancel=this.props.onCancel
    onExit=this.props.onExit
    />
    );



    In alertbox.tsx file :



    import Alert, View from "react-native";
    import * as constants from "../../constants";
    export interface AlertBoxContainerProps
    isAlertVisible?: boolean;
    toggleAlertVisible?: () => any;
    navigation?: any;
    hardwareBackPress?: () => any;
    onExit?: () => any;
    onCancel?: () => any;


    export const AlertBox = (props: AlertBoxContainerProps) =>
    return props.isAlertVisible
    ? (
    Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
    text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
    text: constants.EXIT_TEXT, onPress: () => props.onExit()
    ])
    )
    : null;
    ;


    When I tried this code I got the error : " JSX element type 'void' is not a constructor function for JSX elements.ts(2605) "



    How can I get rid of these errors and render the alert box?



    When I tried with the second approach and by modifying the code I am able to render the Alert, with the following code :



    Approach -3 :



    In alertbox.tsx :



    export const AlertBox = (props: AlertBoxContainerProps) => 
    return (
    <View>

    props.isAlertVisible
    ? Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
    text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
    text: constants.EXIT_TEXT, onPress: () => props.onExit()
    ])
    : null
    </View>
    );


    ;



    But the code is with the weird syntax where in the view I need to place empty ' ' other wise I get the error :



    " Type 'void' is not assignable to type 'ReactNode'.ts(2322)
    index.d.ts(725, 39): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly>' " .










    share|improve this question


























      0












      0








      0








      I am trying to customize the Alert Component. For that in the render() method I am
      wrapping Alert inside the AlerBoxContainer class (that is placed in alertbox-container.tsx file) . I am rendering it by toggling using a prop.



      Approach -1 : By directly returning the Alert component if the condition is true or returning null if condition is false.



      This is in alertbox-container.tsx file:



      import * as React from "react";
      import Alert from "react-native";
      import connect from "react-redux";
      import Dispatch from "redux";
      import State from "../../../../store/reducer";
      import * as constants from "../../constants";
      import * as selectors from "../../selectors";
      import AlertBox, AlertBoxContainerProps from "./alertbox";

      interface AlertBoxState
      showAlert: boolean;
      blurred: boolean;
      appState: any;


      class AlertBoxContainer extends React.Component<AlertBoxContainerProps, AlertBoxState>
      constructor(props)
      super(props);


      render()
      return this.props.isAlertVisible ?
      Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
      text: constants.CANCEL_TEXT, onPress: () => this.props.onCancel() ,
      text: constants.EXIT_TEXT, onPress: () => this.props.onExit()
      ])
      : null;



      const mapStateToProps = (state: State): AlertBoxContainerProps => (
      isAlertVisible: selectors.getIsAlertVisible(state)
      );

      const mapDispatchToProps = (dispatch: Dispatch<core.features.workplace.Action>): AlertBoxContainerProps => (
      toggleAlertVisible: () => dispatch(core.features.workplace.toggleAlertVisible())
      );

      export default connect<any, AlertBoxContainerProps, AlertBoxContainerProps>(
      mapStateToProps,
      mapDispatchToProps
      )(AlertBoxContainer);


      When I tried the above code I got the error :



      Property 'render' in type 'AlertBoxContainer' is not assignable to the same property in base type 'Component'.
      Type '() => void' is not assignable to type '() => ReactNode'.
      Type 'void' is not assignable to type 'ReactNode'.ts(2416)



      I also tried in the following way :



      Aprroach - 2:



      in alertbox-container.tsx :



       render() 
      return (
      <AlertBox
      isAlertVisible=this.props.isAlertVisible
      onCancel=this.props.onCancel
      onExit=this.props.onExit
      />
      );



      In alertbox.tsx file :



      import Alert, View from "react-native";
      import * as constants from "../../constants";
      export interface AlertBoxContainerProps
      isAlertVisible?: boolean;
      toggleAlertVisible?: () => any;
      navigation?: any;
      hardwareBackPress?: () => any;
      onExit?: () => any;
      onCancel?: () => any;


      export const AlertBox = (props: AlertBoxContainerProps) =>
      return props.isAlertVisible
      ? (
      Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
      text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
      text: constants.EXIT_TEXT, onPress: () => props.onExit()
      ])
      )
      : null;
      ;


      When I tried this code I got the error : " JSX element type 'void' is not a constructor function for JSX elements.ts(2605) "



      How can I get rid of these errors and render the alert box?



      When I tried with the second approach and by modifying the code I am able to render the Alert, with the following code :



      Approach -3 :



      In alertbox.tsx :



      export const AlertBox = (props: AlertBoxContainerProps) => 
      return (
      <View>

      props.isAlertVisible
      ? Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
      text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
      text: constants.EXIT_TEXT, onPress: () => props.onExit()
      ])
      : null
      </View>
      );


      ;



      But the code is with the weird syntax where in the view I need to place empty ' ' other wise I get the error :



      " Type 'void' is not assignable to type 'ReactNode'.ts(2322)
      index.d.ts(725, 39): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly>' " .










      share|improve this question
















      I am trying to customize the Alert Component. For that in the render() method I am
      wrapping Alert inside the AlerBoxContainer class (that is placed in alertbox-container.tsx file) . I am rendering it by toggling using a prop.



      Approach -1 : By directly returning the Alert component if the condition is true or returning null if condition is false.



      This is in alertbox-container.tsx file:



      import * as React from "react";
      import Alert from "react-native";
      import connect from "react-redux";
      import Dispatch from "redux";
      import State from "../../../../store/reducer";
      import * as constants from "../../constants";
      import * as selectors from "../../selectors";
      import AlertBox, AlertBoxContainerProps from "./alertbox";

      interface AlertBoxState
      showAlert: boolean;
      blurred: boolean;
      appState: any;


      class AlertBoxContainer extends React.Component<AlertBoxContainerProps, AlertBoxState>
      constructor(props)
      super(props);


      render()
      return this.props.isAlertVisible ?
      Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
      text: constants.CANCEL_TEXT, onPress: () => this.props.onCancel() ,
      text: constants.EXIT_TEXT, onPress: () => this.props.onExit()
      ])
      : null;



      const mapStateToProps = (state: State): AlertBoxContainerProps => (
      isAlertVisible: selectors.getIsAlertVisible(state)
      );

      const mapDispatchToProps = (dispatch: Dispatch<core.features.workplace.Action>): AlertBoxContainerProps => (
      toggleAlertVisible: () => dispatch(core.features.workplace.toggleAlertVisible())
      );

      export default connect<any, AlertBoxContainerProps, AlertBoxContainerProps>(
      mapStateToProps,
      mapDispatchToProps
      )(AlertBoxContainer);


      When I tried the above code I got the error :



      Property 'render' in type 'AlertBoxContainer' is not assignable to the same property in base type 'Component'.
      Type '() => void' is not assignable to type '() => ReactNode'.
      Type 'void' is not assignable to type 'ReactNode'.ts(2416)



      I also tried in the following way :



      Aprroach - 2:



      in alertbox-container.tsx :



       render() 
      return (
      <AlertBox
      isAlertVisible=this.props.isAlertVisible
      onCancel=this.props.onCancel
      onExit=this.props.onExit
      />
      );



      In alertbox.tsx file :



      import Alert, View from "react-native";
      import * as constants from "../../constants";
      export interface AlertBoxContainerProps
      isAlertVisible?: boolean;
      toggleAlertVisible?: () => any;
      navigation?: any;
      hardwareBackPress?: () => any;
      onExit?: () => any;
      onCancel?: () => any;


      export const AlertBox = (props: AlertBoxContainerProps) =>
      return props.isAlertVisible
      ? (
      Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
      text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
      text: constants.EXIT_TEXT, onPress: () => props.onExit()
      ])
      )
      : null;
      ;


      When I tried this code I got the error : " JSX element type 'void' is not a constructor function for JSX elements.ts(2605) "



      How can I get rid of these errors and render the alert box?



      When I tried with the second approach and by modifying the code I am able to render the Alert, with the following code :



      Approach -3 :



      In alertbox.tsx :



      export const AlertBox = (props: AlertBoxContainerProps) => 
      return (
      <View>

      props.isAlertVisible
      ? Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
      text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
      text: constants.EXIT_TEXT, onPress: () => props.onExit()
      ])
      : null
      </View>
      );


      ;



      But the code is with the weird syntax where in the view I need to place empty ' ' other wise I get the error :



      " Type 'void' is not assignable to type 'ReactNode'.ts(2322)
      index.d.ts(725, 39): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly>' " .







      javascript reactjs typescript react-native






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 8:30







      reciever

















      asked Mar 22 at 5:51









      recieverreciever

      116




      116






















          3 Answers
          3






          active

          oldest

          votes


















          0















          remove null from else condition and write a <div /> instead




          Explanation:



          A react component should always return a jsx component, so in your case, if the condition is false, you're returning null, so just return a dummy jsx (say <div /> ) element.






          share|improve this answer























          • As I am using react-native I tried returning dummy <View/> component but no luck, I still get the error : "Property 'render' in type 'AlertBoxContainer' is not assignable to the same property in base type 'Component<AlertBoxContainerProps, AlertBoxState, any>'. Type '() => void | Element' is not assignable to type '() => ReactNode'. Type 'void | Element' is not assignable to type 'ReactNode'. Type 'void' is not assignable to type 'ReactNode'."

            – reciever
            Mar 22 at 6:05












          • in your functional alert component, write return type as ReactNode. And let me know if it works?

            – Siraj
            Mar 22 at 6:11











          • no , I get the error : "Type 'void' is not assignable to type 'ReactNode'.ts(2322) index.d.ts(725, 39): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<PropsWithChildren<ViewProps>>' "

            – reciever
            Mar 22 at 6:15


















          0














           <View>

          props.isAlertVisible
          && Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
          text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
          text: constants.EXIT_TEXT, onPress: () => props.onExit()
          ])

          </View>


          If null is creating the issue then you could do a short-circuit
          Hope it helps !






          share|improve this answer























          • please see the last approach I tried ...it is same as you suggested . There I am not facing any issues with null and I am able to render the Alert box but I want to remove the empty " " symbol in the <view> component as it is syntactically weird.

            – reciever
            Mar 22 at 6:32



















          0














          This issue is that Alert.alert() is not a Component, it’s a function (calling the alert() method). The render function must be passed a Component, so that is why you are getting the errors.



          Alert.alert() is easy to use and handy. It doesn’t have to be in the render method at all to work, in fact it doesn’t even need to be in a Component to work. All you need to do is call it when you want it to come up. For example, here is how you could bring up an alert at the press of a button:



          class Example extends Component<*, *> 
            
          callAlert = () =>
              Alert.alert(
                “Alert title”,
                “Alert explanation”,
                [
                  
                    text: “Yes”,
          onPress: () => console.log(“Yes pressed”),
                  ,
                  
                    text: “No”,
                    onPress: () => console.log('No Pressed'),
                  ,
                ],
                 cancelable: false
              );
            ;

            render()
              return (
                <TouchableOpacity onPress=this.callAlert>
                  <Text>
          Alert Button
                  </Text>
                </TouchableOpacity>
              );
            



          In your case, whenever you toggle your prop, call the alert and everything should be peachy!






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293632%2fhow-to-render-alert-component-conditionally-in-react-native%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0















            remove null from else condition and write a <div /> instead




            Explanation:



            A react component should always return a jsx component, so in your case, if the condition is false, you're returning null, so just return a dummy jsx (say <div /> ) element.






            share|improve this answer























            • As I am using react-native I tried returning dummy <View/> component but no luck, I still get the error : "Property 'render' in type 'AlertBoxContainer' is not assignable to the same property in base type 'Component<AlertBoxContainerProps, AlertBoxState, any>'. Type '() => void | Element' is not assignable to type '() => ReactNode'. Type 'void | Element' is not assignable to type 'ReactNode'. Type 'void' is not assignable to type 'ReactNode'."

              – reciever
              Mar 22 at 6:05












            • in your functional alert component, write return type as ReactNode. And let me know if it works?

              – Siraj
              Mar 22 at 6:11











            • no , I get the error : "Type 'void' is not assignable to type 'ReactNode'.ts(2322) index.d.ts(725, 39): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<PropsWithChildren<ViewProps>>' "

              – reciever
              Mar 22 at 6:15















            0















            remove null from else condition and write a <div /> instead




            Explanation:



            A react component should always return a jsx component, so in your case, if the condition is false, you're returning null, so just return a dummy jsx (say <div /> ) element.






            share|improve this answer























            • As I am using react-native I tried returning dummy <View/> component but no luck, I still get the error : "Property 'render' in type 'AlertBoxContainer' is not assignable to the same property in base type 'Component<AlertBoxContainerProps, AlertBoxState, any>'. Type '() => void | Element' is not assignable to type '() => ReactNode'. Type 'void | Element' is not assignable to type 'ReactNode'. Type 'void' is not assignable to type 'ReactNode'."

              – reciever
              Mar 22 at 6:05












            • in your functional alert component, write return type as ReactNode. And let me know if it works?

              – Siraj
              Mar 22 at 6:11











            • no , I get the error : "Type 'void' is not assignable to type 'ReactNode'.ts(2322) index.d.ts(725, 39): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<PropsWithChildren<ViewProps>>' "

              – reciever
              Mar 22 at 6:15













            0












            0








            0








            remove null from else condition and write a <div /> instead




            Explanation:



            A react component should always return a jsx component, so in your case, if the condition is false, you're returning null, so just return a dummy jsx (say <div /> ) element.






            share|improve this answer














            remove null from else condition and write a <div /> instead




            Explanation:



            A react component should always return a jsx component, so in your case, if the condition is false, you're returning null, so just return a dummy jsx (say <div /> ) element.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 22 at 5:57









            SirajSiraj

            1,75411631




            1,75411631












            • As I am using react-native I tried returning dummy <View/> component but no luck, I still get the error : "Property 'render' in type 'AlertBoxContainer' is not assignable to the same property in base type 'Component<AlertBoxContainerProps, AlertBoxState, any>'. Type '() => void | Element' is not assignable to type '() => ReactNode'. Type 'void | Element' is not assignable to type 'ReactNode'. Type 'void' is not assignable to type 'ReactNode'."

              – reciever
              Mar 22 at 6:05












            • in your functional alert component, write return type as ReactNode. And let me know if it works?

              – Siraj
              Mar 22 at 6:11











            • no , I get the error : "Type 'void' is not assignable to type 'ReactNode'.ts(2322) index.d.ts(725, 39): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<PropsWithChildren<ViewProps>>' "

              – reciever
              Mar 22 at 6:15

















            • As I am using react-native I tried returning dummy <View/> component but no luck, I still get the error : "Property 'render' in type 'AlertBoxContainer' is not assignable to the same property in base type 'Component<AlertBoxContainerProps, AlertBoxState, any>'. Type '() => void | Element' is not assignable to type '() => ReactNode'. Type 'void | Element' is not assignable to type 'ReactNode'. Type 'void' is not assignable to type 'ReactNode'."

              – reciever
              Mar 22 at 6:05












            • in your functional alert component, write return type as ReactNode. And let me know if it works?

              – Siraj
              Mar 22 at 6:11











            • no , I get the error : "Type 'void' is not assignable to type 'ReactNode'.ts(2322) index.d.ts(725, 39): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<PropsWithChildren<ViewProps>>' "

              – reciever
              Mar 22 at 6:15
















            As I am using react-native I tried returning dummy <View/> component but no luck, I still get the error : "Property 'render' in type 'AlertBoxContainer' is not assignable to the same property in base type 'Component<AlertBoxContainerProps, AlertBoxState, any>'. Type '() => void | Element' is not assignable to type '() => ReactNode'. Type 'void | Element' is not assignable to type 'ReactNode'. Type 'void' is not assignable to type 'ReactNode'."

            – reciever
            Mar 22 at 6:05






            As I am using react-native I tried returning dummy <View/> component but no luck, I still get the error : "Property 'render' in type 'AlertBoxContainer' is not assignable to the same property in base type 'Component<AlertBoxContainerProps, AlertBoxState, any>'. Type '() => void | Element' is not assignable to type '() => ReactNode'. Type 'void | Element' is not assignable to type 'ReactNode'. Type 'void' is not assignable to type 'ReactNode'."

            – reciever
            Mar 22 at 6:05














            in your functional alert component, write return type as ReactNode. And let me know if it works?

            – Siraj
            Mar 22 at 6:11





            in your functional alert component, write return type as ReactNode. And let me know if it works?

            – Siraj
            Mar 22 at 6:11













            no , I get the error : "Type 'void' is not assignable to type 'ReactNode'.ts(2322) index.d.ts(725, 39): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<PropsWithChildren<ViewProps>>' "

            – reciever
            Mar 22 at 6:15





            no , I get the error : "Type 'void' is not assignable to type 'ReactNode'.ts(2322) index.d.ts(725, 39): The expected type comes from property 'children' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<PropsWithChildren<ViewProps>>' "

            – reciever
            Mar 22 at 6:15













            0














             <View>

            props.isAlertVisible
            && Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
            text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
            text: constants.EXIT_TEXT, onPress: () => props.onExit()
            ])

            </View>


            If null is creating the issue then you could do a short-circuit
            Hope it helps !






            share|improve this answer























            • please see the last approach I tried ...it is same as you suggested . There I am not facing any issues with null and I am able to render the Alert box but I want to remove the empty " " symbol in the <view> component as it is syntactically weird.

              – reciever
              Mar 22 at 6:32
















            0














             <View>

            props.isAlertVisible
            && Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
            text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
            text: constants.EXIT_TEXT, onPress: () => props.onExit()
            ])

            </View>


            If null is creating the issue then you could do a short-circuit
            Hope it helps !






            share|improve this answer























            • please see the last approach I tried ...it is same as you suggested . There I am not facing any issues with null and I am able to render the Alert box but I want to remove the empty " " symbol in the <view> component as it is syntactically weird.

              – reciever
              Mar 22 at 6:32














            0












            0








            0







             <View>

            props.isAlertVisible
            && Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
            text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
            text: constants.EXIT_TEXT, onPress: () => props.onExit()
            ])

            </View>


            If null is creating the issue then you could do a short-circuit
            Hope it helps !






            share|improve this answer













             <View>

            props.isAlertVisible
            && Alert.alert(constants.ALERT_HEADER, constants.ALERT_MESSAGE, [
            text: constants.CANCEL_TEXT, onPress: () => props.onCancel() ,
            text: constants.EXIT_TEXT, onPress: () => props.onExit()
            ])

            </View>


            If null is creating the issue then you could do a short-circuit
            Hope it helps !







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 22 at 6:26









            ishab acharyaishab acharya

            1009




            1009












            • please see the last approach I tried ...it is same as you suggested . There I am not facing any issues with null and I am able to render the Alert box but I want to remove the empty " " symbol in the <view> component as it is syntactically weird.

              – reciever
              Mar 22 at 6:32


















            • please see the last approach I tried ...it is same as you suggested . There I am not facing any issues with null and I am able to render the Alert box but I want to remove the empty " " symbol in the <view> component as it is syntactically weird.

              – reciever
              Mar 22 at 6:32

















            please see the last approach I tried ...it is same as you suggested . There I am not facing any issues with null and I am able to render the Alert box but I want to remove the empty " " symbol in the <view> component as it is syntactically weird.

            – reciever
            Mar 22 at 6:32






            please see the last approach I tried ...it is same as you suggested . There I am not facing any issues with null and I am able to render the Alert box but I want to remove the empty " " symbol in the <view> component as it is syntactically weird.

            – reciever
            Mar 22 at 6:32












            0














            This issue is that Alert.alert() is not a Component, it’s a function (calling the alert() method). The render function must be passed a Component, so that is why you are getting the errors.



            Alert.alert() is easy to use and handy. It doesn’t have to be in the render method at all to work, in fact it doesn’t even need to be in a Component to work. All you need to do is call it when you want it to come up. For example, here is how you could bring up an alert at the press of a button:



            class Example extends Component<*, *> 
              
            callAlert = () =>
                Alert.alert(
                  “Alert title”,
                  “Alert explanation”,
                  [
                    
                      text: “Yes”,
            onPress: () => console.log(“Yes pressed”),
                    ,
                    
                      text: “No”,
                      onPress: () => console.log('No Pressed'),
                    ,
                  ],
                   cancelable: false
                );
              ;

              render()
                return (
                  <TouchableOpacity onPress=this.callAlert>
                    <Text>
            Alert Button
                    </Text>
                  </TouchableOpacity>
                );
              



            In your case, whenever you toggle your prop, call the alert and everything should be peachy!






            share|improve this answer



























              0














              This issue is that Alert.alert() is not a Component, it’s a function (calling the alert() method). The render function must be passed a Component, so that is why you are getting the errors.



              Alert.alert() is easy to use and handy. It doesn’t have to be in the render method at all to work, in fact it doesn’t even need to be in a Component to work. All you need to do is call it when you want it to come up. For example, here is how you could bring up an alert at the press of a button:



              class Example extends Component<*, *> 
                
              callAlert = () =>
                  Alert.alert(
                    “Alert title”,
                    “Alert explanation”,
                    [
                      
                        text: “Yes”,
              onPress: () => console.log(“Yes pressed”),
                      ,
                      
                        text: “No”,
                        onPress: () => console.log('No Pressed'),
                      ,
                    ],
                     cancelable: false
                  );
                ;

                render()
                  return (
                    <TouchableOpacity onPress=this.callAlert>
                      <Text>
              Alert Button
                      </Text>
                    </TouchableOpacity>
                  );
                



              In your case, whenever you toggle your prop, call the alert and everything should be peachy!






              share|improve this answer

























                0












                0








                0







                This issue is that Alert.alert() is not a Component, it’s a function (calling the alert() method). The render function must be passed a Component, so that is why you are getting the errors.



                Alert.alert() is easy to use and handy. It doesn’t have to be in the render method at all to work, in fact it doesn’t even need to be in a Component to work. All you need to do is call it when you want it to come up. For example, here is how you could bring up an alert at the press of a button:



                class Example extends Component<*, *> 
                  
                callAlert = () =>
                    Alert.alert(
                      “Alert title”,
                      “Alert explanation”,
                      [
                        
                          text: “Yes”,
                onPress: () => console.log(“Yes pressed”),
                        ,
                        
                          text: “No”,
                          onPress: () => console.log('No Pressed'),
                        ,
                      ],
                       cancelable: false
                    );
                  ;

                  render()
                    return (
                      <TouchableOpacity onPress=this.callAlert>
                        <Text>
                Alert Button
                        </Text>
                      </TouchableOpacity>
                    );
                  



                In your case, whenever you toggle your prop, call the alert and everything should be peachy!






                share|improve this answer













                This issue is that Alert.alert() is not a Component, it’s a function (calling the alert() method). The render function must be passed a Component, so that is why you are getting the errors.



                Alert.alert() is easy to use and handy. It doesn’t have to be in the render method at all to work, in fact it doesn’t even need to be in a Component to work. All you need to do is call it when you want it to come up. For example, here is how you could bring up an alert at the press of a button:



                class Example extends Component<*, *> 
                  
                callAlert = () =>
                    Alert.alert(
                      “Alert title”,
                      “Alert explanation”,
                      [
                        
                          text: “Yes”,
                onPress: () => console.log(“Yes pressed”),
                        ,
                        
                          text: “No”,
                          onPress: () => console.log('No Pressed'),
                        ,
                      ],
                       cancelable: false
                    );
                  ;

                  render()
                    return (
                      <TouchableOpacity onPress=this.callAlert>
                        <Text>
                Alert Button
                        </Text>
                      </TouchableOpacity>
                    );
                  



                In your case, whenever you toggle your prop, call the alert and everything should be peachy!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 22 at 8:48









                mstigglemstiggle

                1256




                1256



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293632%2fhow-to-render-alert-component-conditionally-in-react-native%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현