【问题标题】:React component not rendering props coming from parent反应组件不渲染来自父级的道具
【发布时间】:2017-05-28 15:54:21
【问题描述】:
 class CyInfo extends Component {

    foo(){
        console.log(this.props.id);
        return getAttributes(this.props.id)
    }

    render() {
        return ( <Info data = {this.foo()}> </Info>)
    }
}

此父级接收“props.id”并将数据值传递给 getAttributes() 返回的子级。

export default class Info extends Component {
    constructor(props){
        super(props)
    }

    /*componentWillReceiveProps(nextProps){
        console.log(nextProps);
    */

    render() {
        console.log(this.props.data);
        return (
            <div id="info">{this.props.data}</div>
        )
    }
}

在孩子身上,我可以在控制台和 componentWillReceiveProps 中看到 props 值。但数组未呈现。 我尝试使用 react-devtool。在 react-devtool 中,道具似乎通过了孩子但没有渲染。有趣的是,当我更改数组的某些元素时,react-devtool 正在渲染。

我做错了什么。

编辑: [React-Devtool 截图][1]

我编辑了 react-devtool 截图。道具似乎但组件仅呈现初始值。在屏幕截图控制台错误是 favicon 只是忽略此

EDIT2:Console prints props array

编辑 3: JSON.stringify(this.props.data)

【问题讨论】:

  • 您在控制台上看到任何错误/警告吗?传递的数据是什么?它是一个数组..什么样的数组..你想显示什么?
  • 试试{JSON.stringify(this.props.data)}。它可能不显示的原因是它不是文本,而是一个 JavaScript 对象。
  • 我用 react-devtool 和控制台截图编辑了这个问题

标签: javascript reactjs react-jsx


【解决方案1】:

道具来自异步调用方法的函数(getattributes),当这个道具传递给孩子时,没有渲染。 我直接在父子中调用异步方法,并在 componentWillReceiveProps 中使用回调设置状态:

componentWillReceiveProps(nextProps){
    self = this;
    AsyncFunc(nextProps.id ,(error, result) => {
        self.setState({data:result})
    });
}

并使用

进行渲染
    return (<div id="info">
        {Array.isArray(this.state.data) && this.state.data.map((data) => {
            return <div key={data._id}>{data.class}{data.predicate}{data.yuklem}</div>
        })}</div>
    )

【讨论】:

    【解决方案2】:

    由于foo 是一个函数,您必须将其传递给子组件:

    return ( <Info data = {() => this.foo()}> </Info>)
    

    另外,data是一个array,你必须使用.map()进行渲染,如下:

    export default class Info extends Component {
        constructor(props){
            super(props)
        }
    
        /*componentWillReceiveProps(nextProps){
            console.log(nextProps);
        */
    
        render() {
            console.log(this.props.data);
            return (
               <div id="info">{this.props.data.map(( element, index ) => {
                console.log(element);
                <span key={index}> {element}</span>})}
               </div>
            )
        }
    }
    

    【讨论】:

    • 我通过这种方式传递了道具,但没有改变
    • 什么console.log(this.props.data);打印?
    • 我编辑了截图 react-devtool。控制台打印也是相同的数组
    • 感谢回复,我猜元素会在跨度中{元素}。但仍会打印初始值。我在父组件中获得道具的地方重要吗?我在 CyInfo 的父级中有一个 cytoscape 对象,它是 cy.on()(async) 触发的 redux 调度,CyInfo 使用 mapStateToProps 获取这个道具。
    • 对于 devtool 和控制台 props 数组中的所有内容,我不明白为什么它不渲染
    【解决方案3】:

    正如您所提到的,this.data.props 返回一个数组,并且为了渲染数组中的元素,您需要映射数组元素并在渲染之前检查 data is an array or not 最初的值可能不是可用或不可用数组

    export default class Info extends Component {
        constructor(props){
            super(props)
        }
    
        /*componentWillReceiveProps(nextProps){
            console.log(nextProps);
        */
    
        render() {
            console.log(this.props.data);
            return (
                <div id="info">
                   {this.props.data && Array.isArray(this.props.data) && this.props.data.map((data, index) => {
                          return <div key={index}>{data}</div>
                   })}</div>
            )
        }
    }
    

    【讨论】:

    • 控制台警告:数组或迭代器中的每个孩子都应该有一个唯一的“key”道具。我尝试在
      中打印“
    • {data}
    • ”。仍然警告继续,在渲染中当我说 console.log(index,data) 时,控制台打印“0,init value”
  • 警告可以通过在 map 返回的元素上使用键来消除。无论地图中最外层的元素是什么,您都需要在其上添加键
  • 我为
    添加了密钥。现在警告已消除,但仍然不渲染数组,并且 console.log(index,data) 仍然打印“0,init value”。
  • 什么是初始值
  • "gokhan" 是初始值
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签