使用`@ emotion / core`和最新版本的React时,在CodeSandbox中不是“ React.createContext不是函数” >>

问题描述 投票:0回答:1

我正在使用@emotion/core。它可以在我的机器上运行,但是当我尝试在codesandbox中使用它时,出现错误:

React.createContext不是函数

关于此错误的其他帖子可以通过更新react版本来解决。但是代码沙箱使用的是最新版本。

这是我的沙箱:https://codesandbox.io/s/emotion-mp0zc这是我的代码:

import React from 'react';
import styled from '@emotion/styled';
import { css } from '@emotion/core'
/* eslint-disable */

class CompoundInterestGraph extends React.Component {
    state = {
        principal: 3,
        R: 5,
        n: 12,
        t: 30,
        numLattes: 1,
        retirement: 0,
    };
    componentDidMount () {

    }
    setValue(e) {
        this.setState({ numLattes: e });
    }

    constructor(props) {
        super(props);
        const { numLattess, retirement, principal, R, n, t } = this.props;
        this.state = {
            principal: 3,
            R: 5,
            n: 12,
            t: 30,
            numLattes: 1,
            retirement: 0,
        };
        this.setValue = this.setValue.bind(this);
    }

    handleInputChange = event => {
        const { name, value } = event.target;
        this.setState({ [name]: value });
    };

    compoundInterest = (principal, R, n, t) => {
        const r = R > 1 ? R / 100 : R;
        var frac = r / n;
        var compoundInterestForPrincipal = Math.pow(1 + frac, n * t);

        var futureValueOfSeries = (compoundInterestForPrincipal - 1) / (r / n);

        compoundInterestForPrincipal *= principal;
        futureValueOfSeries *= principal;
        var future401kValue =
            compoundInterestForPrincipal + futureValueOfSeries;

        return (
            '$' +
            future401kValue.toLocaleString(undefined, {
                maximumFractionDigits: 2,
            })
        );
    };

    handleChange = function(event) {
        var costOfnumLattes = 3;

        this.setState({
            numLattes: event.target.value,
            principal: event.target.value * costOfnumLattes * 4,
        });
    };

    render() {
        const { principal, R, n, t, numLattes, retirement } = this.state;

        return [
            <div key="0">
                <div>
                    You are drinking <b>{numLattes}</b> less latte a week
                </div>
                <Input
                    id="typeinp"
                    type="range"
                    min="0"
                    max="10"
                    value={numLattes}
                    onChange={this.handleChange.bind(this)}
                    step="1"
                />
            </div>,
            <Div key="1">
                <b>{this.compoundInterest(principal, R, n, t)}</b>
            </Div>,
            <Table key="2">
                <tbody>
                    <tr className="ui header">
                        <th>Principal</th>
                    </tr>
                    <tr>
                        <td>
                            <form>
                                <input
                                    type="text"
                                    placeholder={principal}
                                    onChange={this.handleInputChange}
                                    disabled="disabled"
                                />
                            </form>
                        </td>
                    </tr>
                    <tr className="ui header">
                        <th>Rate of return</th>
                    </tr>
                    <tr>
                        <td>
                            <form>
                                <input
                                    type="text"
                                    placeholder="5%"
                                    onChange={this.handleInputChange}
                                    name="R"
                                    value={R}
                                />
                            </form>
                        </td>
                    </tr>
                    <tr className="ui header">
                        <th>Number of times interest is compounded</th>
                    </tr>
                    <tr>
                        <td>
                            <form>
                                <input
                                    type="text"
                                    placeholder="12"
                                    onChange={this.handleInputChange}
                                    name="n"
                                    value={n}
                                />
                            </form>
                        </td>
                    </tr>
                    <tr className="ui header">
                        <th>Investment Duration (years)</th>
                    </tr>
                    <tr>
                        <td>
                            <form>
                                <input
                                    type="text"
                                    placeholder="30"
                                    onChange={this.handleInputChange}
                                    name="t"
                                    value={t}
                                />
                            </form>
                        </td>
                    </tr>
                </tbody>
            </Table>,
            <div key = "4">
              <b>How did we get these numbers?</b>

              <div className ={equation}>
                <p>
                P (1 +
                <p className={fraction}>
                  <span className="num">1</span>
                  <span className="symbol">/</span>
                  <span className="bottom">2</span>
                </p>
                )
                 <sup>(nt)</sup>
                   </p>
              </div>
            </div>
        ];
    }
}
const Table = styled.table`
    margin-top: 100px;
    width: 30%;
`;

const fraction = css`
    display: inline-block;
    position: relative;
    vertical-align: middle;
    letter-spacing: 0.0001em;
    text-align: center;

    .num {
      display: block;
      padding: 0;
      height: 12px;
      line-height: 12px;
    }

    .bottom {
      border-top: thin solid black;
    }

    .symbol {
      display: none;
    }
`
const equation = css`
p {
    font-size:large !important;
  }
`;

const Input = styled.input`
    width: 30% !important;
`;

const Div = styled.label`
    display: flex;
    align-items: flex-end;
    align-self: flex-end;
    text-align: right;
    width: 30%;
`;
export default CompoundInterestGraph;

我正在使用@ emotion / core。它可以在我的机器上运行,但是当我尝试在codesandbox中使用它时,出现错误:React.createContext不是一个函数关于此错误的其他帖子由...

reactjs emotion codesandbox
1个回答
1
投票

我想我可以使它起作用。

© www.soinside.com 2019 - 2024. All rights reserved.