0

enter image description hereenter image description hereI am very new to React and I can not solve this issue for a very long time. The idea is the following, I get some user info from database and pass it into cookies. From react side I get this cookies as JSON object and I need to render menu for user depending on values in this object. Right now I pass this object that I get from cookies from parent Component and I try to access it from Settings component. The issue is that I can see access this object from test function, however when I try to access this data from return it gives me undefined error. The function that returns value from cookies is sync, I have no idea why that might happen, please help.....enter image description here

8
  • In test function you are accesing this.state.shopSetting whereas in your component, you are accessing this.state.shopSettings (with an extra S at the end). What's weird is that test should log undefined. Commented Jul 6, 2020 at 2:29
  • I made a typo before making screenshot, it should be this.state.shopSettings in test, so no issues on that Commented Jul 6, 2020 at 2:32
  • Okey, what's the value of this.state.shopSettings if you console.log it in the render() function? It's possible that 'new_orders' key does not exist in the object. Commented Jul 6, 2020 at 2:33
  • When I call console.log(this.state.shopSettings) where enabled is defined I get expected object, new_orders is present in keys Commented Jul 6, 2020 at 2:36
  • Also it logs it two times, maybe that could help... Commented Jul 6, 2020 at 2:37

2 Answers 2

2
  1. Since this.state.shopSettings["new_orders"] is boolean, use ternary.
  2. Don't copy props to the state inside constructor as the constructor is executed only once. So updates to the props won't be reflected in the component.

Like this

<button onClick={this.test}>
  {props.shopSettings && (props.shopSettings["new_orders"] ? 'true button' : 'false button')}
</button>
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately did not help, it seems that this code overloaded my computer and browser, it became very laggy. Dont know why that happened. I get them as object for 100% since I use Cookies.getJSON
ok - try {this.state.shopSettings && this.state.shopSettings)["new_orders"]} in the <button> ... if it doesn't work , then share the output of console.log ... also share more code if possible
Did not work, I have attached output of console.log(this.state.shopSettings) inside render. Also if try console.log(this.state.shopSettings['new_orders']) I get undefined
Also send project structure and component which gives this variable
This works as expected! Thank you, but is it possible to access this variables somehow? I will need to change them if user clicks on a button and so on, maybe there is better solution than cookies? I can build an api but I have questions about authentications, I run everything using next and koa right now
0

It solves very easily with this code, now I can access any key from shopSettings inside return without any issues

class Index extends React.Component {
    state = {
        shopSettings: Cookies.getJSON('shopSettings')
    }

    render() {
        const {shopSettings} = this.state

        if (!shopSettings) {
            return null
        }
        
        return (
          <div>Hello</div>
        )
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.