1

I have a component that I am using that has a list that, if an item on it is clicked, will output certain data related to that item. The react class looks like:

SocialMenu = React.createClass({

    getInitialState: function(){
        return { focused: 0 };
    },

    clicked: function(index){

        // The click handler will update the state with
        // the index of the focused menu entry

        this.setState({focused: index});
    },

    render: function() {

        // Here we will read the items property, which was passed
        // as an attribute when the component was created

        var self = this;

        // The map method will loop over the array of menu entries,
        // and will return a new array with <li> elements.

        return (
            <div>
                <ul className="testblocks">{ this.props.items.map(function(m, index){

                    var style = '';

                    if(self.state.focused == index){
                        style = 'focused';
                    }

                    // Notice the use of the bind() method. It makes the
                    // index available to the clicked function:

                    return <li key={index} className={style} onClick={self.clicked.bind(self, index)}>{m}</li>;

                }) }

                </ul>

                <p>Selected: {this.props.items[this.state.focused]}</p> 

            </div>
        );

    }
});

EDIT:

Component would look like this I suppose:

ItemDetails = React.createClass({

    render: function() {

        return (
            <div>{this.props.item}</div>
        );
    }

});

This works fine when I put it in the SocialMenu component, but if I want to put it anywhere else on the page, the output is undefined. How do I deal with this?

EDIT:

I am trying to set it up so it goes in a component like this:

Description = React.createClass({

    getInitialState: function() {
        return { default: true };
    },

    render: function() {

        return (

            <section id="one" className="main style1">
                <div className="container">

                    <ItemDetails />

                    <span className="image fit">
                      <img src="images/pic01.jpg" alt="" />
                    </span>

                </div>
            </section>

        );

    } 

});

and the <ItemDetails /> component should show the relevant output related to the click on the specific item in the list in SocialMenu. If I just feed it props (<ItemDetails items={['items', 'some more'}/>), it will just print out what it is fed.

If I put SocialMenu in the Description component, then the list is showing in that section (which I do not want).

6
  • Just... create another component then? What were you having trouble with? Commented Mar 3, 2016 at 6:45
  • how do I get that component to inherit: Commented Mar 3, 2016 at 6:56
  • {this.props.items[this.state.focused]} Commented Mar 3, 2016 at 6:56
  • This is the same problem we talked about before on another question (can't seem to find it, is it deleted?). You'll have to do some communication between components. Either through one parent component or something like Flux to communicate over component boundaries. Commented Mar 3, 2016 at 7:12
  • Here it is: stackoverflow.com/questions/35729466/… So basically, you choose either that approach or you separate your communication into Flux, or some other 'message' system where you'd subscribe. Given the abundance of good Flux implementations I'd suggest reading into that. It solves exactly this problem for you, if you're unable to put a mother component around the two. Commented Mar 3, 2016 at 7:14

2 Answers 2

1

You can move this code into a new component that takes the current item as a prop.

You've already got the current item via this.props.items[this.state.focused] so pass this as a prop to your new component

<ItemDetails item={ this.props.items[this.state.focused] } />

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

7 Comments

how would I write the ItemDetails component?
so, I created the component, but I cannot put it in another component (another section) because it is not being fed the item data. Error is: Uncaught TypeError: Cannot read property 'undefined' of undefined
<ItemDetails item={ this.props.items[this.state.focused] } /> This works in the SocialMenu component, but not in other components, how do I overcome this?
Are the items passes as props to your other components like they are in the SocialMenu component?
Not as of now, how would I do that?
|
0

There is really nice article by Dan Abramov(creator of Redux) regarding Separating presentational and container components.

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.