账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    React使用map方法的时候遇到个奇怪的问题,请求高手知道
    46
    0
    class BlackBorderContainer extends React.Component {
            render(){
                console.log(this.props.children[0].props); //object
                this.props.children.map((val,i)=>{
                    console.log(val);
                    return <div key={i} className="black-board">{val.type}</div>
                })
            }
        }
        ReactDOM.render(
            <BlackBorderContainer>
                <div className='name'>My Name:Lucy</div>
                <p className='age'> My Age:<span>12</span></p>
            </BlackBorderContainer>,
            document.getElementById('example')
        )

    我2个console打印出来的内容 可以看出有三个对象,那我这里{val.type}为什么还是错误的?
    难道说是因为<BlackBorderContainer>组件内部的不对称,一个是div一个是p的缘故?
    {className: "name", children: "My Name:Lucy"}
    {$$typeof: Symbol(react.element), type: "div", key: null, ref: null, props: {…}, …}
    {$$typeof: Symbol(react.element), type: "p", key: null, ref: null, props: {…}, …}
    从中可以看出我最后return的时候 val是正确输出的,但是程序会报错
    Uncaught Error: BlackBorderContainer.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部