0

Is it possible to set the data for companyLogo as a const without saving to state? And have that variable available within the render function to pass as props to a component as you see below.

The data for companyLogo is only available once a user logs in so please note the use of the componentDidMount function.

I'm sure there is a more elegant way of doing this, just unsure how.

@connect((store) => {
    return {
        user: store.user
    }
})

export default class Header extends Component {
    constructor() {
        super();

        this.state = {
            companyLogo: null
        }
    }

    componentDidMount() {
        //Get company logo and store to pass as props
        this.setState({
            companyLogo: this.props.user.user.groups[0].logoUrl
        })
    }

  render() {
    return (
        <div className="header-container">
            <Row className="header-body">
                <Branding companyLogo={this.state.companyLogo} />
                <AskQuestion />
                <Navigation />
                <Profile />
            </Row>
        </div>
    )
  }
}
2
  • 1
    <Branding companyLogo={this.props.user.user.groups[0].logoUrl} /> ? Commented Jun 14, 2017 at 9:05
  • Unfortunately not, please not the componentDidMount as the data is not available on load. Commented Jun 14, 2017 at 9:07

2 Answers 2

2

what is the value props of company logo before the component mount? you could add a validation on render instead.

{
   this.props.user.user.groups && this.props.user.user.groups[0].logoUrl &&
  <Branding companyLogo={this.props.user.user.groups[0].logoUrl} />
}

so the render for company logo only occurs when the value is available.

Sign up to request clarification or add additional context in comments.

4 Comments

The value of props company logo is only available after the component has mounted hence componentDidMount. Is there anything wrong with the way I'm doing currently? I just thought there may be a better way.
if the company logo is not changing on the component, no need to put it on component's state. just pass it directly to the child. do the validation on render. that's how I would do it.
Please see the edit to your question - this is my solution code. Many thanks!
export default class Header extends Component { render() { const companyLogo = this.props.user.user.groups[0].logoUrl; return ( <div className="header-container"> <Row className="header-body"> {companyLogo && <Branding companyLogo={companyLogo} /> } <AskQuestion /> <Navigation /> <Profile /> </Row> </div> ) } }
1

Preparing state for component from props is not advisable, for instance, if your user logo changes in store, it will not update the logo in your component. If you really want to use props in state, you will have to use componentWillRecieveProps to update State when user logo change in store, so that component can update accordingly, or directly use props as mentioned in other comment

p.s. sorry my reputation is too low to comment

3 Comments

Thank you for your answer. My solution code is in the above comments - would you have any advisories for that approach?
in your solution, you are not checking for const companyLogo = this.props.user.user.groups[0].logoUrl;
if his.props.user.user is undefined, javascript will throw an error, so , you can parse keys safely using if conditions

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.