After doing some tutorials and reading the documentation, I'm trying to setup my first react project to try and get a real understanding of how it all works. So I'm a real beginner at this, And I have a feeling I'm not grasping a fundamental concept.
I'm having an issue trying to pass an object from a child component to it's parent. I've managed to pass the object to the parent, but can't then write it to the parent's state.
I may well be approaching this in completely the wrong way. any guidance would be apricated.
I've updated my code to now use a ref to pass the object to the parent However, I assuming because the refs are only processes by React after the HTML is rendered its allways passing back the last item in my object array not the one i want to associate with each selection.
class AppContainer extends React.Component { //parent Component
constructor() {
super();
this.state = {
contactsList:
[
{id:1, name: 'Tom Brace', phone: '0123456789', address:'fg dfgh dfgh dfgh dfgh', notes: 'sdfdsfasdfasdf asdf as df asdf sadf a sdfasdf', Quote:''},
...
{id:7, name: 'Dave Johnson', phone: '0123456789', address:'fg dfgh dfgh dfgh dfg', notes: 'sdfdsfasdfasdf asdf as df asdf sadf a sdfasdf', Quote:''}
],
selectedContact: {id:1, name: 'Tom Brace', phone: '0123456789', address:'fg dfgh dfgh dfgh dfg', notes: 'sdfdsfasdfasdf asdf as df asdf sadf a sdfasdf', Quote:''}
}
}
render(){
return(
<div className="container-fluid">
<div className="row">
<div id="sidebar" className="col-xs-12 col-md-3 sidebar">
<ContactNav updateContact={this._updateContact.bind(this)}
contactsList={this.state.contactsList}
/>
</div>
<div id="content" className="col-xs-12 col-md-9 main">
<Content
selectedContact={this.state.selectedContact}
/>
</div>
</div>
</div>
);
};
_updateContact(obj){
console.log(obj)
this.setState({
selectedContact: obj
});
}
}
class ContactNav extends React.Component { //child of AppContainer
render() {
const contacts = this._getContacts() || [];
return(
<div>
<div className="input-group">
<input type="text" className="form-control" placeholder="Search for..." />
<span className="input-group-btn">
<button className="btn btn-default" type="button">Go!</button>
</span>
</div>
<ul className="nav nav-sidebar">
{contacts}
</ul>
</div>
);
}
_handleClick(){
event.preventDefault();
console.log(this._obj);
let obj = this._obj
this.props.updateContact(obj);
}
_getContacts() {
return this.props.contactsList.map((i) => {
return (
<li key={i.id}>
<a href="#" key={i.id} onClick={this._handleClick.bind(this)} ref={(input) => this._obj = i}>
{i.name}
</a>
</li>
);
});
}
}
class Content extends React.Component { //child of AppContainer
render() {
return(
<div>
<h1 className="page-header">{this.props.selectedContact.name}</h1>
<h3 className="sub-header">{this.props.selectedContact.phone}</h3>
<h6 className="sub-header">{this.props.selectedContact.address}</h6>
<h6 className="sub-header">{this.props.selectedContact.notes}</h6>
</div>
);
}
}
ReactDOM.render(
<AppContainer />, document.getElementById('app')
);
html, body, #app, .container-fluid, .row {
height: 100%;
}
.sidebar {
z-index: 1000;
display: block;
padding: 20px;
overflow-x: hidden;
overflow-y: auto;
background-color: #f5f5f5;
border-right: 1px solid #eee;
height:100%;
}
.active {
background-color: blue;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div id="app">
</div>
onClick={this.updateContact.bind(this)}for the ContactNav click handler?