<div id="root"></div>
body {
  height: 100vh;
  margin: 0;
  display: grid;
  place-items: center;
}

.box {
  border: 1px solid black;
  margin: 10px;
  padding: 10px;
  width: 300px;
}
View Compiled
const { useState, useMemo } = React;

const MyComponent = () => {
  const [num, setNum] = useState(0);
  
  const getFactorial = val => {
    if (val === 0) return 1;
    return val * getFactorial(val - 1);
  };
  
  const calculatedFactorial = useMemo(() => {
    return num ? getFactorial(num) : 0;
  }, [num]);
  
  return (
    <div>
      <section className="box">
        <div>Enter a number:</div>
        <input value={num} onChange={(e) =>
          setNum(e.target.value)}
        />
      </section>
      <div className="box">Factorial: {calculatedFactorial}</div>
    </div>
  );
}

ReactDOM.render(<MyComponent />, document.getElementById('root'));
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js