如何在React打字稿中使用镭(错误:与'CSSProperties'类型没有共同的属性。TS2559)?

时间:2019-02-10 07:04:46

标签: reactjs typescript web

我正在尝试在React中使用Typescript。现在需要使用Radium进行一些样式设置。

我知道这是关于jsx类型的样式,不允许使用媒体, 但我不知道如何解决。 有人可以帮忙吗? 非常感谢。

运行服务器出现错误

Failed to compile
/.../my_react/ts-react/src/Person/Person.tsx
Type error: Type '{ '@media (min-width: 500px)': { width: string; }; }' has no properties in common with type 'CSSProperties'.  TS2559

    17 |     };
    18 |     return (
  > 19 |         <div className="Person" style={style}>
       |                                 ^
    20 |             <p onClick={props.click}>I'm {props.name} and I am {props.age} years old!</p>
    21 |             <p>{props.children}</p>
    22 |             <input type="text" onChange={props.change} value={props.name}/>
This error occurred during the build time and cannot be dismissed.

我已经安装了Radium npm install --save @types/radium 并导入它。

我的package.json

{
  "name": "ts-react",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@types/jest": "24.0.0",
    "@types/node": "10.12.24",
    "@types/radium": "^0.24.2",
    "@types/react": "16.8.2",
    "@types/react-dom": "16.8.0",
    "radium": "^0.25.1",
    "react": "^16.8.1",
    "react-dom": "^16.8.1",
    "react-scripts": "2.1.3",
    "typescript": "3.3.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

//目标ts文件

const person = (props: any) => {
    const style = {
        '@media (min-width: 500px)': {
            width: '450px'
        }
    };
    return (
        <div className="Person" style={style}>
            <p onClick={props.click}>I'm {props.name} and I am {props.age} years old!</p>
            <p>{props.children}</p>
            <input type="text" onChange={props.change} value={props.name}/>
        </div>
    );
};


export default Radium(person);

//根ts文件

import React, { Component, useState } from 'react';
import Radium from 'radium';

import './App.css';
import Person from './Person/Person';


class App extends Component {
  state = {
    persons: [
      { id: 0, name: 'John', age: 30},
      { id: 1, name: 'Jack', age: 20},
      { id: 2, name: 'Joe', age: 40},
    ],
    show: false
  }

  deletePersonHandler = (index: number) => {
    const persons = [...this.state.persons];
    persons.splice(index, 1);
    this.setState({
      ...this.state,
      persons: persons
    });
  }

  nameChangedHandler = (event: React.ChangeEvent<HTMLInputElement>, id: number) => {
    const personIndex = this.state.persons.findIndex(p => {
      return p.id === id;
    });

    const person = {
      ...this.state.persons[personIndex]
    };
    person.name = event.target.value;
    const persons = [...this.state.persons];
    persons[personIndex] = person;

    this.setState({
      ...this.state,
      persons: persons
    });
  }

  togglePersonsHandler = () => {
    const doesShow = this.state.show;
    this.setState({
      ...this.state,
      show: !doesShow
    });
  }

  render() {
    const style = {
      backgroundColor: 'green',
      color: 'white',
      font: 'inherit',
      border: '1px solid blue',
      padding: '8px',
      cursor: 'pointer',
      ':hover': {
        backgroundColor: 'lightgreen',
        color: 'black'
      }
    };

    let contents = null;
    if (this.state.show) {
      contents = (
        <Radium.StyleRoot>
          <div>
            {this.state.persons.map((person, index) => {
              return <Person
                click={this.deletePersonHandler.bind(this, index)}
                change={(event: React.ChangeEvent<HTMLInputElement>) => this.nameChangedHandler(event, person.id)}
                name={person.name}
                age={person.age}
                key={person.id} />
            })}
          </div>
        </Radium.StyleRoot>
      );
      style.backgroundColor = 'red';
      style[':hover'] = {
        backgroundColor: 'salmon',
        color: 'black'
      }
    }

    const classes = [];

    if (this.state.persons.length <= 2) {
      classes.push('red');
    }
    if (this.state.persons.length <= 1) {
      classes.push('bold');
    }


    return (
      <div className="App">
        <h1>Hi</h1>
        <p className={classes.join(' ')}>This is working!</p>
        <button 
          style={style}
          onClick={this.togglePersonsHandler}>Switch Name</button>
        {contents}
      </div>
    );
  }
}

export default Radium(App);

1 个答案:

答案 0 :(得分:2)

在person函数组件中,您可以将样式类型声明为Radium.StyleRules,如下所示。

const style:Radium.StyleRules = {
  "@media (min-width: 500px)": {
    width:"450px",
  },
};