1

I am trying to make a component with a dynamic size based on its props. Currently, I am testing it with the following setup, however when I adjust the slider, the component doesn't change size

// View.js
import React, { Component } from 'react';
import Cell from './Cell'

export default class View extends Component {
  constructor(props) {
    super(props);

    this.handleSlide = this.handleSlide.bind(this);

    this.state = {
      cellSize: 50
    };
  }

  handleSlide(event) {
    this.setState({
      cellSize: event.target.value
    });
  }

  render() {
    return (
      <div>
        <input type="range" min="1" max="100" value={this.state.cellSize} onChange={this.handleSlide} />

        <Cell size={this.state.cellSize}/>

      </div>
    );
  }
}
// Cell.js
import React, { Component } from 'react';

export default class Cell extends Component {

  render() {
    const cellSize = {
      width: this.props.size,
      height: this.props.size
    }

    return (
      <div style={cellSize}>
      </div>
    );
  }
}

What am I doing wrong

1 Answer 1

1

You are missing units for the width and height properties. You can use template literals:

const cellSize = {
   width: `${this.props.size}px`,
   height: `${this.props.size}px`,
};

or string concatenation:

const cellSize = {
   width: this.props.size + 'px',
   height: this.props.size + 'px',
};
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.