简体   繁体   English

this.props在子组件的console.log中工作,但不渲染

[英]this.props working in console.log of child component, but not rendering

I'm passing a recently updated state variable to a child component. 我正在将最近更新的状态变量传递给子组件。 In that child component I'm able to console.log(this.props.response.message) and see the expected result of the recently updated variable. 在该子组件中,我可以console.log(this.props.response.message)并查看最近更新的变量的预期结果。 However, the this.props.response.message is not rendering. 但是, this.props.response.message不会呈现。 Do I need to perform some action in a React Lifecycle method? 我需要在React Lifecycle方法中执行一些操作吗? If so, which one and how would that look? 如果是这样,那会是什么样子?

Parent Component 父组件

 import React, { Component } from 'react'; import axios from 'axios'; import FormSuccess from './FormSuccess'; export default class NewRuleForm extends Component { constructor(props) { super(); this.state = { submitSuccess: false, isOpen: false, response: {} }; this.handleSubmit = this.handleSubmit.bind(this); } async handleSubmit(e) { let response = await axios.post('localhost:5000', { data: e.target.id.value } ) if (response.data.success) { this.setState({ submitSuccess: true, response: response.data }) } else { this.setState({ submitSuccess: false, }) } return response } } render() { if (this.state.submitSuccess === true) { return <FormSuccess result={this.state.response} /> } return ( <form onSubmit={this.handleSubmit}> <input type="text" name="id"/> </form> ) } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> 

Child Component 子组件

 import React, { Component } from 'react'; export default class FormSuccess extends Component { constructor(props) { super(); } render() { console.log(this.props.result.message) // works return ( <div> <h1>Successful Form Submission</h1> {/* below doesn't render */} <p>{this.props.result.message}</p> </div> ); } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> 

Again, in that child component I am able to console.log(this.props.response.message) and see the expected result of the recently updated variable. 同样,在该子组件中,我可以console.log(this.props.response.message)并查看最近更新的变量的预期结果。 However, it does not render. 但是,它不会渲染。

You are passing data in result prop, 您正在result道具中传递数据,

<FormSuccess result={this.state.response} />

You should use it like this, 你应该这样使用

{this.props.result} // Normal case

If you are getting object in result and want to retrieve message from it then use this, 如果您要得到result对象,并想从中检索message ,请使用它,

{this.props.result.message}

Also you should be careful about data type. 另外,您应该注意数据类型。 If data type is json or something like that, you cant render it but you can write on console. 如果数据类型是json或类似的内容,则无法渲染它,但可以在控制台上编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在console.log中,状态已更新,也通过道具反映出来,但是该组件未重新呈现 - State is updated, also its reflecting with props, in console.log, however the component is not re-rendering IntelliJ显示this.props未定义,而console.log建议否则 - IntelliJ shows this.props is undefined while console.log suggests otherwise 反应不渲染道具,但可以在console.log中看到 - React not rendering props but can see in console.log 将 this.props 的值推送到子组件的 state - Push this.props' value to the state of child component 除非我在setTimeout内包装console.log(this.state),否则无法在.then(json)提取调用之外访问this.state或this.props - Cannot access this.state or this.props outside the .then(json) fetch call unless i wrap console.log(this.state) inside setTimeout 为什么子组件不显示console.log输出? - why child component not showing console.log Outputs? 如何在 console.log 中打印 props 值? - How to print the props value in console.log? 无法使用 Link 控制台记录道具 - Unable to console.log props using Link this.props不是从父组件到子组件的函数还原 - this.props is not a function redux from parent to child component console.log()无法按预期工作 - console.log() not working as expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM