After I click the submit button a query goes to a Neo4j DB and the response is sent back. Also, when the button is click another function is sending that data to the Graph function {this.state.submit && this.displayElements()}
. Once in the Graph function I trying to render a graph but React is saying map is undefined. However, a console log of props from inside Graph shows: {data:{action: "Changed", timestamp: 1499348050, object: Array(1), Second: Array(1), __typename: "action", …}
. I have tried different combination before map but nothing seems to work (this.props.nodes, this.state.action, etc).
Display Function (Sending data via props as nodes)
displayElements = () => {
const {data:{loading, error, action}} = this.props;
console.log(this.props);
if(loading) {
return <option disabled>Loading...</option>;
}else if (error){
return<p>Error!</p>;
}
return (
<Graph nodes={this.data}/> <--Sending data to Graph via props as nodes
);
}
Graph Function
class Graph extends React.Component {
constructor(props) {
super(props);
this.state = {nodes: this.props};
console.log(this.props + "graph")
}
componentDidMount() {
this.force = d3.forceSimulation(this.state.nodes)
.force("charge",
d3.forceManyBody()
.strength(this.props.forceStrength)
)
.force("x", d3.forceX(this.props.width / 2))
.force("y", d3.forceY(this.props.height / 2));
this.force.on('tick', () => this.setState({nodes: this.state.nodes}));
}
componentWillUnmount() {
this.force.stop();
}
render() {
return (
<svg width={this.props.width} height={this.props.height}>
{this.state.nodes.action.map((node, index) =>(
<circle r={node.r} cx={node.x} cy={node.y} fill="red" key={index}/>
))}
</svg>
);
}
}
export default Graph;
map is for an array, but your passing objects {data:{action: "Changed", timestamp: 1499348050, object: Array(1), Second: Array(1), __typename: "action", …}
A solution can be Object.keys(this.state.nodes.action).map(()=>{...}
{Object.keys(this.state.nodes.action).map((node, index) =>(
<circle r={node.r} cx={node.x} cy={node.y} fill="red" key={index}/>
))}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.