Access the child value in JSON Objects react native











up vote
0
down vote

favorite












I have this function to fetch the user data such in the code below.



constructor(props){
super(props);
this.state = {
user_id: this.props.navigation.state.params.user_id,
user: null
}
}

componentWillMount(){
this.loadUserDetail()
}

loadUserDetail(){
AccountController.loadUserDetail(this.state.user_id).then(data=>{
if(data.status == true){
this.setState({ user: data.user })
}
})
}


From the loadUserDetail() function, I set the user data in a state and the this.state.user result is as follow :



{ company_name: 'Universal Sdn Bhd', address: 'test', company_desc: 'test' }


So I'm trying to display these data in my render() by directly specifying the object like this.state.user.company_name but it doesn't work.



Here is how my rendering looked like :



render() {
return (
<View style={{flex: 1}}>
<Text style={{ fontSize: 20, fontWeight: 'bold' }}>{ this.state.user.company_name }</Text>
</View>
)
}


By displaying the data like in the code, it throws me this error




TypeError: null is not an object (evaluating 'this.state.user.company_name')




I'm able to display the data if I store every specific value in a state after fetching the data in the loadUserDetail() function but if let say I have a lot of data in my array then I have to set every each of them in a state and I want to avoid doing so.










share|improve this question






















  • FWIW: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON.
    – T.J. Crowder
    Nov 7 at 8:52















up vote
0
down vote

favorite












I have this function to fetch the user data such in the code below.



constructor(props){
super(props);
this.state = {
user_id: this.props.navigation.state.params.user_id,
user: null
}
}

componentWillMount(){
this.loadUserDetail()
}

loadUserDetail(){
AccountController.loadUserDetail(this.state.user_id).then(data=>{
if(data.status == true){
this.setState({ user: data.user })
}
})
}


From the loadUserDetail() function, I set the user data in a state and the this.state.user result is as follow :



{ company_name: 'Universal Sdn Bhd', address: 'test', company_desc: 'test' }


So I'm trying to display these data in my render() by directly specifying the object like this.state.user.company_name but it doesn't work.



Here is how my rendering looked like :



render() {
return (
<View style={{flex: 1}}>
<Text style={{ fontSize: 20, fontWeight: 'bold' }}>{ this.state.user.company_name }</Text>
</View>
)
}


By displaying the data like in the code, it throws me this error




TypeError: null is not an object (evaluating 'this.state.user.company_name')




I'm able to display the data if I store every specific value in a state after fetching the data in the loadUserDetail() function but if let say I have a lot of data in my array then I have to set every each of them in a state and I want to avoid doing so.










share|improve this question






















  • FWIW: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON.
    – T.J. Crowder
    Nov 7 at 8:52













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have this function to fetch the user data such in the code below.



constructor(props){
super(props);
this.state = {
user_id: this.props.navigation.state.params.user_id,
user: null
}
}

componentWillMount(){
this.loadUserDetail()
}

loadUserDetail(){
AccountController.loadUserDetail(this.state.user_id).then(data=>{
if(data.status == true){
this.setState({ user: data.user })
}
})
}


From the loadUserDetail() function, I set the user data in a state and the this.state.user result is as follow :



{ company_name: 'Universal Sdn Bhd', address: 'test', company_desc: 'test' }


So I'm trying to display these data in my render() by directly specifying the object like this.state.user.company_name but it doesn't work.



Here is how my rendering looked like :



render() {
return (
<View style={{flex: 1}}>
<Text style={{ fontSize: 20, fontWeight: 'bold' }}>{ this.state.user.company_name }</Text>
</View>
)
}


By displaying the data like in the code, it throws me this error




TypeError: null is not an object (evaluating 'this.state.user.company_name')




I'm able to display the data if I store every specific value in a state after fetching the data in the loadUserDetail() function but if let say I have a lot of data in my array then I have to set every each of them in a state and I want to avoid doing so.










share|improve this question













I have this function to fetch the user data such in the code below.



constructor(props){
super(props);
this.state = {
user_id: this.props.navigation.state.params.user_id,
user: null
}
}

componentWillMount(){
this.loadUserDetail()
}

loadUserDetail(){
AccountController.loadUserDetail(this.state.user_id).then(data=>{
if(data.status == true){
this.setState({ user: data.user })
}
})
}


From the loadUserDetail() function, I set the user data in a state and the this.state.user result is as follow :



{ company_name: 'Universal Sdn Bhd', address: 'test', company_desc: 'test' }


So I'm trying to display these data in my render() by directly specifying the object like this.state.user.company_name but it doesn't work.



Here is how my rendering looked like :



render() {
return (
<View style={{flex: 1}}>
<Text style={{ fontSize: 20, fontWeight: 'bold' }}>{ this.state.user.company_name }</Text>
</View>
)
}


By displaying the data like in the code, it throws me this error




TypeError: null is not an object (evaluating 'this.state.user.company_name')




I'm able to display the data if I store every specific value in a state after fetching the data in the loadUserDetail() function but if let say I have a lot of data in my array then I have to set every each of them in a state and I want to avoid doing so.







json object react-native






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 8:28









Eaten Taik

135212




135212












  • FWIW: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON.
    – T.J. Crowder
    Nov 7 at 8:52


















  • FWIW: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON.
    – T.J. Crowder
    Nov 7 at 8:52
















FWIW: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON.
– T.J. Crowder
Nov 7 at 8:52




FWIW: JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON.
– T.J. Crowder
Nov 7 at 8:52












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Your render needs to allow for the fact that it can be called when this.state.user is null and show an appropriate display for that case, since your component will not have the user until/unless the asynchronous operation completes. For instance:



render() {
const { user } = this.state;
return (
<View style={{flex: 1}}>
{user
? <Text style={{ fontSize: 20, fontWeight: 'bold' }}>{ user.company_name }</Text>
: <Text style={{ fontSize: 20, fontStyle: 'italic' }}>loading</Text>
}
</View>
)
}


If you don't want your component to show a pending state for when it doesn't have user, the call to load the user should be in the parent component instead, and it should only render the child when the user is known (passing it as props).





Side note: Inline style is generally a maintenance hassle. Consider using classes instead.



Side note 2: Presumably, loadUserDetail can fail. You should have a catch handler on it to deal with the failure.






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',
    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%2f53185763%2faccess-the-child-value-in-json-objects-react-native%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Your render needs to allow for the fact that it can be called when this.state.user is null and show an appropriate display for that case, since your component will not have the user until/unless the asynchronous operation completes. For instance:



    render() {
    const { user } = this.state;
    return (
    <View style={{flex: 1}}>
    {user
    ? <Text style={{ fontSize: 20, fontWeight: 'bold' }}>{ user.company_name }</Text>
    : <Text style={{ fontSize: 20, fontStyle: 'italic' }}>loading</Text>
    }
    </View>
    )
    }


    If you don't want your component to show a pending state for when it doesn't have user, the call to load the user should be in the parent component instead, and it should only render the child when the user is known (passing it as props).





    Side note: Inline style is generally a maintenance hassle. Consider using classes instead.



    Side note 2: Presumably, loadUserDetail can fail. You should have a catch handler on it to deal with the failure.






    share|improve this answer



























      up vote
      0
      down vote



      accepted










      Your render needs to allow for the fact that it can be called when this.state.user is null and show an appropriate display for that case, since your component will not have the user until/unless the asynchronous operation completes. For instance:



      render() {
      const { user } = this.state;
      return (
      <View style={{flex: 1}}>
      {user
      ? <Text style={{ fontSize: 20, fontWeight: 'bold' }}>{ user.company_name }</Text>
      : <Text style={{ fontSize: 20, fontStyle: 'italic' }}>loading</Text>
      }
      </View>
      )
      }


      If you don't want your component to show a pending state for when it doesn't have user, the call to load the user should be in the parent component instead, and it should only render the child when the user is known (passing it as props).





      Side note: Inline style is generally a maintenance hassle. Consider using classes instead.



      Side note 2: Presumably, loadUserDetail can fail. You should have a catch handler on it to deal with the failure.






      share|improve this answer

























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Your render needs to allow for the fact that it can be called when this.state.user is null and show an appropriate display for that case, since your component will not have the user until/unless the asynchronous operation completes. For instance:



        render() {
        const { user } = this.state;
        return (
        <View style={{flex: 1}}>
        {user
        ? <Text style={{ fontSize: 20, fontWeight: 'bold' }}>{ user.company_name }</Text>
        : <Text style={{ fontSize: 20, fontStyle: 'italic' }}>loading</Text>
        }
        </View>
        )
        }


        If you don't want your component to show a pending state for when it doesn't have user, the call to load the user should be in the parent component instead, and it should only render the child when the user is known (passing it as props).





        Side note: Inline style is generally a maintenance hassle. Consider using classes instead.



        Side note 2: Presumably, loadUserDetail can fail. You should have a catch handler on it to deal with the failure.






        share|improve this answer














        Your render needs to allow for the fact that it can be called when this.state.user is null and show an appropriate display for that case, since your component will not have the user until/unless the asynchronous operation completes. For instance:



        render() {
        const { user } = this.state;
        return (
        <View style={{flex: 1}}>
        {user
        ? <Text style={{ fontSize: 20, fontWeight: 'bold' }}>{ user.company_name }</Text>
        : <Text style={{ fontSize: 20, fontStyle: 'italic' }}>loading</Text>
        }
        </View>
        )
        }


        If you don't want your component to show a pending state for when it doesn't have user, the call to load the user should be in the parent component instead, and it should only render the child when the user is known (passing it as props).





        Side note: Inline style is generally a maintenance hassle. Consider using classes instead.



        Side note 2: Presumably, loadUserDetail can fail. You should have a catch handler on it to deal with the failure.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 7 at 8:38

























        answered Nov 7 at 8:30









        T.J. Crowder

        667k11611771273




        667k11611771273






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185763%2faccess-the-child-value-in-json-objects-react-native%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()