0

I'm fairly new to the react side of life and have come across a slightly annoying issue with my Syntax that I could do with a little help on.

The premise is very simple:

I am trying to pass an object as a prop to a Component.

Parent Element: -- Trying to pass the state to the Totals component

class Dash_overview extends React.Component{
    constructor(props){
        super(props)

        this.state = {
            companies: {
                title: 'companies on record',
                value: null,
                messurement: 'Companies'
            },
            earning: {
                title: 'total earning',
                value: null,
                messurement: 'mill'
            }
        }
    }
    render(){
        return (
            <div className="overview-container">
                <div className="totals">
                    <Totals values={this.state.companies}/>
                    <Totals values={this.state.earning}/>
                </div>
            </div>
        )
    }
}

Child Component -- which is going to use the values passed to it

class Totals extends React.Component{
    constructor(props){
        super(props)
        this.state = {
            
        }
    }
    render(){
        return (
            <div className="totals_comp">
                <h3>{companies.title}</h3>
                <h3>{companies.value}</h3>
                <h3>{companies.messurement}</h3>
            </div>
        )
    }
}

--

Im probably making a silly mistake but I have tried a few different variations of this without success so would really value someone pointing out where I am going wrong. :)

Thanks in advance, Wally

3 Answers 3

1

You can spread the state values into the child component props, the object keys will be the prop names used within the component.

<Totals {...this.state.companies}/>
<Totals {...this.state.earning}/>

Or explicitly pass the prop values

const { messurement, title, value } = this.state.companies;

...

<Totals
  messurement={messurement}
  title={title}
  value={value}
/>
<Totals
  messurement={messurement}
  title={title}
  value={value}
/>

And then in the child access via props

<div className="totals_comp">
  <h3>{this.props.title}</h3>
  <h3>{this.props.value}</h3>
  <h3>{this.props.messurement}</h3>
</div>

Issue

values={this.state.companies} takes the state object value and assigns it to a prop named values, but then in the child component you don't reference it at all. i.e. like props.values.title.

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

4 Comments

no don't, pass them as a named parameter and grab them as expected instead
@Qchmqs When spread the object keys are used to provide the prop name, which matches what is being accessed in the component.
which isn't very predictable with two objects prone to change at any moment, and doesn't even address the real question, which is how to access the props
@Qchmqs Please read the included reason for issue and entire suggested solution, which covers reading/accessing the passed props.
1

Since you are passing {title: 'companies on record',value: null,messurement: 'Companies'} as values prop, you should consume values from the other component. if you want to use companies name do this :

<div className="overview-container">
        <div className="totals">
            <Totals companies={this.state.companies}/>
            <Totals companies={this.state.earning}/>
        </div>
    </div>

and then do this on Totals component:

const {companies}=this.props
render(){
  return (
    <div className="totals_comp">
        <h3>{companies.title}</h3>
        <h3>{companies.value}</h3>
        <h3>{companies.messurement}</h3>
    </div>
 )}

Comments

0

Try this.

const { title,value,messurement }  = this.props.values;
    
    render(){
        return (
            <div className="totals_comp">
                <h3>{title}</h3>
                <h3>{value}</h3>
                <h3>{messurement}</h3>
            </div>
        )
    }

3 Comments

this worked as intended - thank you for your help - quick question on this answer / why is react unable to take props passed into its components i.e. is this expected to have to define in the component the props you are passing // In Vue.js you have to define an array of props i.e. props=['title', 'value', measurement] - if you have any documentation on this that would be great - for the moment - thanks again for the help :)
@Wally React components are passed a single argument, props, which is an object. You don't need to define the properties (propTypes) of this object, but you need to know/understand what properties are being passed in order to access them. You stuffed all the state into a property called values, so you needed to unstuff them from values. Components and props may help.
In react .props perform as a data communication from parent to child component. When props value change , the component receive that prop rerender agan . Complete guide for your curiosity read documentation here reactjs.org/docs/components-and-props.html

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.