2

I'm trying to format numbers using numeral js - this is working I log number.

How can I access the let number whilst it's outside of the render function to pass down to the element "unit-item-number"?

import React from "react";

import listUnitItem from "./listUnitItem.scss";

//Number formatting
const numeral = require('numeral');

export default class ListUnitItem extends React.Component {
    componentDidMount() {
        //format numbers greater than 1000
        let number = this.props.number;
        if(number > 1000) {
            //only format number
            number = numeral(number).format('0.0a');
        } else {
            return number
        }
        console.log(number);
    }

    render() {
        return(
            <li className="list-unit-item col-sm-2">
                <span className="unit-item-number">{this.props.number}</span>
                <span className="unit-item-title">{this.props.title}</span>
            </li>
        )
    }
}

3 Answers 3

2

While the answer above is valid, it's always good to write modular code and keep your react components stateless (without having state variables). The following code shows a better approach to your problem:

import React from "react";

import listUnitItem from "./listUnitItem.scss";

//Number formatting
const numeral = require('numeral');

export default class ListUnitItem extends React.Component {
    // Save this method in some Utils class where you can access from other components.
    getFormattedNumber(number){
        //format numbers greater than 1000
        if (number > 1000) {
            //only format number
            number = numeral(number).format('0.0a');
        }
        return number
    }

    render() {
        return(
            <li className="list-unit-item col-sm-2">
                <span className="unit-item-number">{this.getFormattedNumber(this.props.number)}</span>
                <span className="unit-item-title">{this.props.title}</span>
            </li>
        )
    }
}

I hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much mate. I'm completely in favour of stateless components as it's a much cleaner way of creating react components. Will mark this as the appropriate solution for others.
That's true! I took some time to get used with this stateless concept! Cheers
2

I would personally put this into state using this.setState({}) in the componentWillMount function.

Something like

export default class ListUnitItem extends React.Component {
    componentWillMount() {
        let number = this.props.number;

        if(number > 1000) {
            number = numeral(number).format('0.0a');
        }

        this.setState({number})
    }

    render() {
        return(
            <li className="list-unit-item col-sm-2">
                <span className="unit-item-number">{this.state.number}</span>
                <span className="unit-item-title">{this.props.title}</span>
            </li>
        )
    }
}

1 Comment

Mate that is perfect! Was close I guess haha. Thanks mate!
1

You don't require any lifecycle method for this, because if any change happens in the props value, formatter will not get applied on that, use it like this:

<li className="list-unit-item col-sm-2">
     <span className="unit-item-number">{this._fromatNum()}</span>
     <span className="unit-item-title">{this.props.title}</span>
</li>

_fromatNum(){
     let number = this.props.number;
     if(number > 1000) {
         number = numeral(number).format('0.0a');
     }
     return number;            
}

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.