0

I have a problem when I wanna get data from state, in console.log appears 2 values. I want remove the empty value, but I've run out of ways. How to remove an empty value?

1

class DetailOrderTracking extends Component {
    constructor(props) {
        super(props)
        this.state = {
            data: []
        }
    }

    componentDidMount = async () => {
        const { query } = this.props.router;
        var getOrderTrackings = await OrderTrackingRepository.getOrderTracking(query.numberbill, query.courier);
        if (getOrderTrackings.ordertracking.status.code == 200) {
            var getManifest = getOrderTrackings.ordertracking.result.manifest;
            this.setState({ data: getManifest });
        }
    }

    render() {
        const { data } = this.state;
        console.log(data) // will print 2 values, first condition is empty value, and second condition has values (an example is in the image above)
        return (
            <div/>
        )
    }
}
4
  • check the length of the data array --> data.length. If its zero, array is empty. Commented Sep 18, 2020 at 15:41
  • 3
    What does that mean, you "want to remove the empty value"? Your component starts out with empty data, and render is called. Then you populate the data, and render is called again at that point… Commented Sep 18, 2020 at 15:42
  • so if length>0 render..... Commented Sep 18, 2020 at 15:43
  • If you are using lodash, you have a method called isEmpty to check or the best way to determine whether the array is empty or not is by the length. Commented Sep 18, 2020 at 15:45

1 Answer 1

2

This should do it:

    render() {
        const { data } = this.state;
        if (data.length > 0){
          console.log(data)
        }
        return (
            <div/>
        )
    }
Sign up to request clarification or add additional context in comments.

Comments

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.