Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ReactJS中更改可用组件中的样式_Reactjs_React Props - Fatal编程技术网

如何在ReactJS中更改可用组件中的样式

如何在ReactJS中更改可用组件中的样式,reactjs,react-props,Reactjs,React Props,我有以下代码 这是我的可重用组件,名称为“块” 另外,我还有另一个组件名为“MainBox”,我想在“MainBox”中使用不同样式的“Blocks”组件。下面您可以看到代码和新样式 import React from "react"; import Blocks from "../Blocks"; import styles from "./MainBox.module.scss"; import cx

我有以下代码

这是我的可重用组件,名称为“块”

另外,我还有另一个组件名为“MainBox”,我想在“MainBox”中使用不同样式的“Blocks”组件。下面您可以看到代码和新样式

    import React from "react";
    import Blocks from "../Blocks";
    import styles from "./MainBox.module.scss";
    import cx from "classnames";
    function MainBox() {
      return (
        <div>
          <div> Some Text in Main Box  </div>
          <Blocks newStyle={styles.someStyle} />
        </div>
      );
    }
    export default MainBox;

但是出现了一些问题,没有应用新的样式,没有任何错误,我只是写了一些错误的语法,请帮助我解决这个问题

您可以尝试将样式浓缩如下:

    import React from "react";
    import styles from "./Blocks.module.scss";
    import cx from "classnames";
    const Blocks = ({ newStyle }) => {
      return (
        <div style= {[styles.blocksBox, newStyle ]}>
          <div>
            Some Text !!!
          </div>
        </div>
       );
     };

    export default Blocks;
从“React”导入React;
从“/Blocks.module.scss”导入样式;
从“类名称”导入cx;
常量块=({newStyle})=>{
返回(
一些文字!!!
);
};
导出默认块;
例如:

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';

// You can import from local files
import AssetExample from './components/AssetExample';

// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';

export default function App() {
  return (
    <View style={styles.container}>
      <Box />
      <Box newStyle={{ backgroundColor: 'yellow' }} />
    </View>
  );
}

const Box = ({ newStyle }) => {
  return <View style={[styles.box, newStyle]}></View>;
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  box: {
    backgroundColor: 'red',
    width: 200,
    height: 200,
  },
});
import*as React from'React';
从“react native”导入{Text,View,StyleSheet};
从“expo常量”导入常量;
//可以从本地文件导入
从“./components/AssetExample”导入AssetExample;
//或npm中可用的任何纯javascript模块
从“react native paper”导入{Card};
导出默认函数App(){
返回(
);
}
常量框=({newStyle})=>{
返回;
};
const styles=StyleSheet.create({
容器:{
弹性:1,
为内容辩护:“中心”,
paddingTop:Constants.statusBarHeight,
背景颜色:“#ecf0f1”,
填充:8,
},
方框:{
背景颜色:“红色”,
宽度:200,
身高:200,
},
});
输出:


我刚刚删除了花括号,一切都开始工作了

import React from "react";
import styles from "./Blocks.module.scss";
import cx from "classnames";
const Blocks = ({ newStyle }) => {
  return (
    <div className={cx(styles.blocksBox,  newStyle )}>
      <div>
        Some Text !!!
      </div>
    </div>
   );
 };

export default Blocks;
从“React”导入React;
从“/Blocks.module.scss”导入样式;
从“类名称”导入cx;
常量块=({newStyle})=>{
返回(
一些文字!!!
);
};
导出默认块;

这不起作用,因为
styles
变量是一个对象,它包含从css模块导入的样式的类名,而不是样式本身。当我删除“cx”时,您需要将它们传递给classNames属性,而不是我的所有样式working@AbirTaheer你能给我提供正确的代码吗?太棒了,我还添加了一个工作示例。一定要退房。
    import React from "react";
    import styles from "./Blocks.module.scss";
    import cx from "classnames";
    const Blocks = ({ newStyle }) => {
      return (
        <div style= {[styles.blocksBox, newStyle ]}>
          <div>
            Some Text !!!
          </div>
        </div>
       );
     };

    export default Blocks;
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';

// You can import from local files
import AssetExample from './components/AssetExample';

// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';

export default function App() {
  return (
    <View style={styles.container}>
      <Box />
      <Box newStyle={{ backgroundColor: 'yellow' }} />
    </View>
  );
}

const Box = ({ newStyle }) => {
  return <View style={[styles.box, newStyle]}></View>;
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  box: {
    backgroundColor: 'red',
    width: 200,
    height: 200,
  },
});
import React from "react";
import styles from "./Blocks.module.scss";
import cx from "classnames";
const Blocks = ({ newStyle }) => {
  return (
    <div className={cx(styles.blocksBox,  newStyle )}>
      <div>
        Some Text !!!
      </div>
    </div>
   );
 };

export default Blocks;