首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在尝试在react中使用Radium

我正在尝试在react中使用Radium
EN

Stack Overflow用户
提问于 2020-04-06 11:16:36
回答 1查看 442关注 0票数 1

所以我有两个组件,Person和App,person组件有一些内联CSS样式,我要在react中使用它,但我得到了以下错误。

Module '"../../../../../../Users/7oss/Desktop/ReactTutorials/my-first-portfolio/node_modules/@types/radium"' has no exported member 'StyleRoot'.ts(2305)

我正在尝试将@media样式添加到Person组件。

我尝试使用Radium.StyleRoot而不是StyleRoot,但得到了以下错误:

代码语言:javascript
复制
Objects are not valid as a React child (found: object with keys {@media (min-width: 500px)}). If you meant to render a collection of children, use an array instead.

下面是App组件:

代码语言:javascript
复制
 import React, { Component, ChangeEvent } from "react";
import "./App.css";
import Person from "./Components/Person/Person";
import Radium, { StyleRoot } from "radium";

class App extends Component {
  state = {
    persons: [
      { id: "hoba", name: "Hosam", age: 24 },
      { id: "hoba1", name: "Ayah", age: 18 },
      { id: "hoba2", name: "Test", age: 20 }
    ],
    ShowPersons: false
  };

  deletePersonHandler = (personIndex: any) => {
    // const persons = this.state.persons.slice(); this is one way
    const persons = [...this.state.persons]; // Another way
    persons.splice(personIndex, 1);
    this.setState({ persons: persons });
  };

  nameChangeHandler = (event: any, id: any) => {
    // console.log('Was clicked!!');
    // this.state.persons[0].name = "7ossam" DON'T DO THIS!!! USE SETSTATE()

    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({
      persons: persons
    });
  };

  togglePersonsHanddler = () => {
    const doesShow = this.state.ShowPersons;
    this.setState({ ShowPersons: !doesShow });
  };

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

    let persons = null;

    if (this.state.ShowPersons) {
      persons = (
        <div>
          {this.state.persons.map((person, index) => {
            return (
              <Person
                name={person.name}
                age={person.age}
                click={() => this.deletePersonHandler(index)}
                key={person.id}
                changedName={(event: any) =>
                  this.nameChangeHandler(event, person.id)
                }
              />
            );
          })}
        </div>
      );
      style.backgroundColor = "red";
      style[":hover"] = {
        backgroundColor: "salmon",
        color: "black"
      };
    }

    let classes = [];

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

    return (
      <StyleRoot>
        <div className="App">
          <br />
          <p className={classes.join(" ")}>Yasta garab el hoba hoba</p>
          <button style={style} onClick={this.togglePersonsHanddler}>
            Toggle Names
          </button>

          <br />
          <h1>Hello, this is sam!</h1>
          {persons}
        </div>
      </StyleRoot>
    );
  }
}

export default Radium(App);

下面是Person组件:

代码语言:javascript
复制
import React, { Component } from "react";
import Radium from "radium";
import "./Person.css";

interface IPersonProps {
  name: string;
  age: number;
  click?: any;
  changedName?: any;
}

class Person extends Component<IPersonProps> {
  render() {
    const style = {
      "@media (min-width: 500px)": {
        width: "450px"
      }
    };
    return (
      <div className="Person">
        {" "}
        style={style}
        <p onClick={this.props.click}>
          {" "}
          I am {this.props.name} and I am {this.props.age} years old
        </p>
        <p>{this.props.children}</p>
        <input
          type="text"
          onChange={this.props.changedName}
          value={this.props.name}
        />
      </div>
    );
  }
}

export default Radium(Person);

以下是位于package -lock.json中的radium包:

代码语言:javascript
复制
 "@types/radium": {
      "version": "0.24.2",
      "resolved": "https://registry.npmjs.org/@types/radium/-/radium-0.24.2.tgz",
      "integrity": "sha512-AudCpKQH/csx6eB4OZhEdKf8Avm18wX8gLOig5H5iocPDPp3GRUPkQmUOXvsIYO64LyAb4CiIfSmcWUaUdvl4A==",
      "requires": {
        "@types/react": "*"
      }
    },

我假设问题出在StyleRoot没有以某种方式正确导入,但我希望能提供一些意见。我也在使用TypeScript

EN

回答 1

Stack Overflow用户

发布于 2020-11-04 02:19:28

我也有同样的问题。我设法通过显式地使用常量样式来解决这个问题。

代码语言:javascript
复制
const style: Radium.StyleRules = {
    '@media (min-width: 500px)': {
        width: '450px'
    }
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61052453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档