如何在 react 的數字 input 加入逗號

Sep 7, 2018, 1 min read

Image

class Foo extends Component {
  state = {
    price: 100,
  }

  onPriceChange = (e) => {
    const priceWithComma = e.target.value;
    const price = parseInt(priceWithComma.split(',').join(''), 10);
    this.setState({ price });
  }

  render() {
    return (
      <input
        value={this.state.price.toLocaleString()}
        onChange={this.onPriceChange}
      />
    );
  }
}

Category react

Tags react, js

Comments