ReactJS / Javascript:React.createElement期望字符串但传递对象

时间:2018-08-25 20:15:51

标签: javascript neo4j create-react-app react-apollo graphql-js

当我尝试用数据渲染图形时,React存在问题。该页面为空白,但控制台向我显示此错误消息:

  

警告:React.createElement:类型无效-预期为字符串   (对于内置组件)或类/函数(对于复合   组件),但得到了:对象。

我正在Apollo和graphql的帮助下向Neo4j DB发送查询,并希望显示一些结果。上面的错误来自我的Graph组件(UserList.js)

UserList.js

class Graph extends React.Component {

    constructor({data}) {
        //console.log('construc data' ,data);
        const times = d3.extent(data.action.map(action => action.timestamp))
        console.log('construc data' ,times);
        const range = [50, 450]
        super({data})
        this.scale = d3.time.scale().domain(times).range(range)
        this.state = {data, times, range}
        //console.log('state' ,this.data);
    }

    componentDidMount() {
        let group
        const { target } = this.refs

        const Canvas = ({children}) => 
    <svg height="200" width="500">
        {children}
    </svg>



        group.append('circle')
    .attr('cy', 160)
    .attr('r', 5)
    .style('fill', 'blue')

group.append('text')
    .text(d => d.year + " - " + d.event)
    .style('font-size', 10)
    .attr('y', 115)
    .attr('x', -95)
    .attr('transform', 'rotate(-45)')


    const TimelineDot = ({position, txt}) =>
    <g transform={`translate(${position},0)`}>

        <circle cy={160} 
                r={5} 
                style={{fill: 'blue'}} />

        <text y={115} 
              x={-95} 
              transform="rotate(-45)" 
              style={{fontSize: '10px'}}>{txt}</text>

    </g>
    }

    render() {
        const { data } = this.state
        const { scale } = this
        return (
            <div className="timeline">
                <h1>{this.props.name} Timeline</h1>
                <Canvas>
                    {data.action.map((action, i) => 
                        <TimelineDot position={scale(action.timestamp)} 
                                     txt={`${action.timestamp} - ${action.action}`}
                        />
                    )}
                </Canvas>
            </div>
        )
    }


}

export default graphql(getObjectsQuery, 
    { options: (ownProps) => { 
      console.log(ownProps.startTime); 
      return ({ second: { startTime: ownProps.startTime,
                              endTime: ownProps.endTime
       } }) 
    } } )(Graph);

2 个答案:

答案 0 :(得分:0)

似乎您在const Canvas =内定义了const TimelineDot =componentDidMount(),因此它们仅是已知的位置。尝试将定义移到render()中以使用它们。

答案 1 :(得分:0)

在“选项”中定义的函数应返回对象:

return { second: { startTime: ownProps.startTime,
                   endTime: ownProps.endTime
   } }

大括号被解释为标记/反应组件(jsx)-对象不是标记/组件然后出错