Reactjs 我试着用镭来反应

Reactjs 我试着用镭来反应,reactjs,typescript,radium,Reactjs,Typescript,Radium,所以我有两个组件,Person和App,Person组件有一些内联CSS样式,我将在react中使用,但是我得到以下错误 Module'../../../../../../../../../Users/7oss/Desktop/ReactTutorials/my first portfolio/node_modules/@types/radium“'没有导出的成员“StyleRoot”.ts(2305) 我正在尝试将@media样式添加到Person组件中 我尝试过使用Radium.StyleR

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

Module'../../../../../../../../../Users/7oss/Desktop/ReactTutorials/my first portfolio/node_modules/@types/radium“'没有导出的成员“StyleRoot”.ts(2305)

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

我尝试过使用Radium.StyleRoot代替StyleRoot,但出现以下错误:

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.
以下是应用程序组件:

 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);
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);


我假设问题在于StyleRoot没有以某种方式正确导入,但我希望您能提供一些信息。我也在使用TypeScript,我也有同样的问题。我通过显式地输入常量样式来解决这个问题

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

我也有同样的问题。我通过显式地输入常量样式来解决这个问题

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