2

I am generating a list of elements with:

LeftPanel.js

if (this.state.trader_or_team == 'trader') {
            itemList = users.map(u => <User key={u.id} user={u}
                                            selected={this.props.selected_trader && this.props.selected_trader.id == u.id}
                                            onClick={this.props.handleTraderSelection.bind(this, u)}
            />);
        } else {
            itemList = teams.map(t => <Team key={t.id} team={t}
                                            selected={this.props.selected_team && this.props.selected_team.id == t.id}
                                            onClick={this.props.handleTeamSelection.bind(this, t)}
            />)
        }

handleTeamSelection/handleTraderSelection are in the parent component:

TargetManager.js

handleTraderSelection(selected_trader) {
        console.log('test')
        this.setState({
            selected_trader
        });
    }

    handleTeamSelection(selected_team) {
        this.setState({
            selected_team
        });
    }

They are passed down as props:

<LeftPanel
                                   handleTraderSelection={::this.handleTraderSelection}
                                   handleTeamSelection={::this.handleTeamSelection}
                        />

And rendered:

LeftPanel.js

return(
    <div className="item-container">
        {itemList}
    </div>
)

When I click on any of the elements nothing happen at all. What is going wrong?


It appears to break when I bind something to it, in the render method of LeftPanel.js:

render() {
    this.props.handleTraderSelection()
    ...

Works,

render() {
    this.props.handleTraderSelection.bind(this)
    ...

Does not work.

2 Answers 2

2

Try binding the handleTraderSelection and handleTeamSelection function like

handleTraderSelection = (selected_trader) => {
        console.log('test')
        this.setState({
            selected_trader
        });
    }

    handleTeamSelection = (selected_team) => {
        this.setState({
            selected_team
        });
    }

I have been following the conventional method to call the parent function like

handleTraderSelection = (value) => {
   this.props.handleTraderSelection(value);
}
handleTeamSelection= (value) => {
   this.props.handleTeamSelection(value);
}
if (this.state.trader_or_team == 'trader') {
            itemList = users.map(u => <User key={u.id} user={u}
                                            selected={this.props.selected_trader && this.props.selected_trader.id == u.id}
                                            onClick={this.handleTraderSelection.bind(this, u)}
            />);
        } else {
            itemList = teams.map(t => <Team key={t.id} team={t}
                                            selected={this.props.selected_team && this.props.selected_team.id == t.id}
                                            onClick={this.handleTeamSelection.bind(this, t)}
            />)
        }

and it works for me well.

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

5 Comments

I get exactly the same result as before :(
Also try <LeftPanel handleTraderSelection={this.handleTraderSelection.bind(this)} handleTeamSelection={this.handleTeamSelection.bind(this)} /> . I dont think that it makes any difference though
Nope, same result, :: is just shorthand for .bind(this)
Your answer was useful so I +1, it's my fault for not putting onClick event on the elements.
No problem, glad that you figured it out]
0

I was missing my onClick for my elements...:

export default class User extends Component {

    render() {
        return (
            <div onClick={this.props.onClick}>
            ...

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.