简体   繁体   English

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

[英]How to use radium in React typescript (Error: has no properties in common with type 'CSSProperties'. TS2559)?

I'm trying to use Typescript in React.我正在尝试在 React 中使用 Typescript。 Now need some style by using Radium.现在需要一些样式使用镭。

I know it's about jsx type of style does not allow to use media, but I don't know how to fix it.我知道这是关于 jsx 类型的样式不允许使用媒体,但我不知道如何解决它。 Anyone can help?任何人都可以帮忙吗? Thanks very much.非常感谢。

run server has some error运行服务器有一些错误

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.

I have installed Radium npm install --save @types/radium and also import it.我已经安装了 Radium npm install --save @types/radium并导入了它。

my package.json我的 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"
  ]
}

// target ts file // 目标 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);

// root ts file // 根 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);

In the person function component, you can declare the style type as Radium.StyleRules like below.在 person 函数组件中,您可以将样式类型声明为 Radium.StyleRules,如下所示。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TS2559:类型 &#39;{children: Element[]; }&#39; 与类型 &#39;IntrinsicAttributes&#39; 没有共同的属性 - TS2559: Type '{ children: Element[]; }' has no properties in common with type 'IntrinsicAttributes' TS2559:类型'{孩子:从不[]; }' 与类型 'IntrinsicAttributes &amp; { post?: IPost | 没有共同的属性。 不明确的; }' - TS2559: Type '{ children: never[]; }' has no properties in common with type 'IntrinsicAttributes & { post?: IPost | undefined; }' React + Typescript:TypeScript错误:“ string”类型与“ CSSProperties”类型没有共同的属性 - React + Typescript : TypeScript error: Type 'string' has no properties in common with type 'CSSProperties' 打字稿反应-定义元素名称锚点的类型-TS2559 - typescript react - define types for a element name anchor - TS2559 类型 &#39;{ 日期样式:字符串; }&#39; 与类型 &#39;DateTimeFormatOptions&#39;.ts(2559) 没有共同的属性 - Type '{ dateStyle: string; }' has no properties in common with type 'DateTimeFormatOptions'.ts(2559) “字符串”类型与“CSSProperties”类型没有共同的属性 - Type 'string' has no properties in common with type 'CSSProperties' 类型'{孩子:元素; }' 与类型 'IntrinsicAttributes &amp; AnimatePresenceProps'.ts(2559) 没有共同的属性 - Type '{ children: Element; }' has no properties in common with type 'IntrinsicAttributes & AnimatePresenceProps'.ts(2559) Typescript React - 与“IntrinsicAttributes”类型没有共同的属性 - Typescript React - has no properties in common with type 'IntrinsicAttributes' “(params: any) =&gt; CSSProperties”类型的值与“Properties”类型没有共同的属性<string | number> '. 你是想打电话给它吗?</string> - Value of type '(params: any) => CSSProperties' has no properties in common with type 'Properties<string | number>'. Did you mean to call it? 如何在TypeScript中使用Radium? - How to use Radium in TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM