Update: This problem seems to be related to mongoose. Please see update at the bottom.
In the parent I use the following code to pass choosenItem
to a grandChild:
childContextTypes: {
foo: React.PropTypes.object.isRequired,
},
getChildContext: function() {
return { foo: this.state.choosenDriver }
},
choosenDriver is a mongoose
Model that looks like this:
var Schema = mongoose.Schema;
var driverSchema = new mongoose.Schema({
driver: String,
age: String,
cars: [{ type: Schema.Types.ObjectId, ref: 'Car' }]
});
module.exports = mongoose.model('Driver', driverSchema);
This works fine and in the GrandChild I can do the following:
contextTypes: {
foo: React.PropTypes.object.isRequired,
},
//some other stuff...
render: function(){
return (
<h1>{this.context.foo.name}</h1>
)
});
And this displays the name of the choosenDriver
.
I would also like to loop through the cars and thought it could be done like this:
render: function(){
var cars = new Array(this.context.foo.cars);
var driversCars = cars.map(function(car,i){
return <li key={i}> {car} </li>;
});
return (
<div>
<h1>{this.context.foo.name}</h1>
<ul>{driversCars}</ul>
</div>
)
}
});
This returns all the drivers cars in a string displayed on one single line. I took a look in react developers tool and saw that cars is not defined as an Array.
Is this maybe because choosenDriver
is a state
and arrays are not supported in this manner?
The cars obviously exists but not as an Array.
Any ideas on how to access cars as an Array in this example?
Thank you!
Update:
When looking at this.context.foo.cars in react-Tools i see this:
cars:
->_proto_ :{..}
0: "4242594395935934",
1: "3543435095340509"
etc...
cars should be an Array so I would expect to see:
cars: Array[4]
Allthough in the DB everythingg looks as it should:
"cars":["5607b0747eb3eefc225aed61","5607b07a7eb3eefc225aed62","5606bf4b0e76916c1d1668b4","5607b07a7eb3eefc225aed62"]}]
Last Update: Here they are next to eachother i react Tools: (I have a list of drivers and this shows that the Driver-object Changes when it gets selected)
State:
choosenDriver: {..}
_v:0
_id: "235345353453"
name: "Roger"
cars:
->_proto_ :{..}
0: "4242594395935934",
_id: undefined
Same driver but not choosen:
drivers: array[3]
->0: {}
->1: {}
_v: 0
_id: "4242594395935934"
cars: Array[1]
So something happens to the Array when it gets selected. Weird...