1

I am using preact(light version of react) but syntax is almost the same. I am having an issue displaying verified after setting state from promise result. This is my container component:

import { h, Component } from "preact";
import { VerifierService } from "services/verifierService";
var CONFIG = require("Config");

//import * as styles from './profile.css';

interface PassportProps { token?: string; path?: string }
interface PassportState { appId?: string; verified?: boolean }

export default class Passport extends Component<PassportProps, PassportState> {
  constructor(props) {
    super(props);

    this.state = { appId: CONFIG.Settings.AppId };
  }

  async componentDidMount() {
    console.log("cdm: " + this.props.token);

    if (this.props.token != undefined) {
      await VerifierService.post({ token: this.props.token })
        .then(data => {
          this.setState({ verified: data.result });
          console.log(JSON.stringify(data, null, 4));
        })
        .catch(error => console.log(error));
    }
  }

  render() {
    return <div>Test: {this.state.verified}</div>;
  }
}

I can see console.log as true inside of promise result, but i can't display it in view.

2
  • 1
    What does your data look like in the console.log? Commented Jun 30, 2018 at 16:45
  • 1
    "true" as boolean Commented Jun 30, 2018 at 16:51

1 Answer 1

2

Your data in your console.log is true, so therefor data.result will give you undefined. Try to just set the data in setState.

await VerifierService.post({ token: this.props.token })
  .then(data => {
    this.setState({ verified: data });
    console.log(JSON.stringify(data, null, 4));
  })
  .catch(error => console.log(error));
Sign up to request clarification or add additional context in comments.

3 Comments

Actually data is true but that doesn’t map with my model since model result is Boolean, have to fix that. Can you confirm life cycle is ok though?
@AmelSalibasic Yes, your code looks fine. Since you are using async/await you could also write const data = await VerifierService.post({ token: this.props.token }); this.setState({ verified: data }); if you think that's more readable.
also boolean cannot be rendered, we have to convert it to string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.