1

I have this code

import React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import 'materialize-css/sass/materialize.scss';
import 'font-awesome/scss/font-awesome.scss';
import Storage from './Storage.js'
import {fetchStorage, loadAddStorageModal, selectStorage} from '../actions/StorageActions.js'


class SideNavContainer extends React.Component {
    componentWillMount() {        
        this.props.fetchStorage();
    }
    render() {
             console.log(this.props);
        return (
            <ul id="slide-out" className="side-nav fixed">      
                {
                    var hold = this.props;
                    this.props.storages.fetchedStorages.map(function (storage, i) {
                        return <Storage key={i} >{storage.name}</Storage>
                    })
                }
                <li>
                    <a className="waves-effect waves-light" onClick={() => this.props.loadAddStorageModal(true)}><i className="fa fa-plus" aria-hidden="true">Add</i></a>
                </li>
                <li className="hide-on-large-only"><a href="/Home/Index">Home</a></li>
                <li className="hide-on-large-only"><a href="/Account/SignOut">Signout</a></li>
            </ul>
        )
    }
}

function mapStateToProps(state) {
    return {
        storages: state.storages
    };
}

function matchDispatchToProps(dispatch){
    return bindActionCreators({fetchStorage: fetchStorage, loadAddStorageModal: loadAddStorageModal, selectStorage: selectStorage }, dispatch);
}


export default connect(mapStateToProps, matchDispatchToProps)(SideNavContainer);

I am trying to pass "this.props" into my Storage Component. I tried to do

 return <Storage {...this.props} key={i} >{storage.name}</Storage>

but I believe scope has changed so that does not work anymore. I tried to put it in a variable called "hold" but that just crashes the entire application as it says "var" is an Unexpected token.

1
  • Try moving the var hold = ... to before the return statement. Commented Aug 10, 2016 at 23:15

1 Answer 1

1

Use the fat arrow style function expression to auto-bind this:

this.props.storages.fetchedStorages.map((storage, i) => {
  return <Storage key={'storage-' + i} {...this.props}>{storage.name}</Storage>;
});
Sign up to request clarification or add additional context in comments.

2 Comments

cool. I was just looking and I think passing all of the props is over kill. How could I just pass in selectStorage from action creator?
I can get it with the using "bind" {this.props.selectStorage.bind(this)} but I can't get it with using auto bind. Maybe I did not do it right

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.