Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.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_Material Ui - Fatal编程技术网

Reactjs 如何将样式从父组件发送到子组件?

Reactjs 如何将样式从父组件发送到子组件?,reactjs,material-ui,Reactjs,Material Ui,我想将样式对象从父组件发送到子组件。 Parent.js: const styles = theme => ({ field: { flexGrow: 1, position: 'relative', zIndex: 1, }, }); class Parent extends React.Component { render(){ const {classes } = this.props return ( <div>

我想将样式对象从父组件发送到子组件。 Parent.js:

const styles = theme => ({

  field: {
    flexGrow: 1,
    position: 'relative',
    zIndex: 1,
  },
});

class Parent extends React.Component {
 render(){
  const {classes } = this.props
  return (
   <div>
    <Child className={classes.field} />
   </div>
  )
}
}
constyles=theme=>({
字段:{
flexGrow:1,
位置:'相对',
zIndex:1,
},
});
类父类扩展了React.Component{
render(){
const{classes}=this.props
返回(
)
}
}
Child.js:

const styles = theme => ({

  container: {
    flexGrow: 1,
    position: 'relative',
    zIndex: 2,
  },
});

class Child extends React.Component {
 render(){
  const {classes} = this.props
  return (
   <div>
    <Child className={classes.field} />
   </div>
  )
}
}
constyles=theme=>({
容器:{
flexGrow:1,
位置:'相对',
zIndex:2,
},
});
子类扩展了React.Component{
render(){
const{classes}=this.props
返回(
)
}
}

这将返回子组件的对象类不包含父组件中包含的样式字段。是否有建议将样式从父级发送到子级?

您可以尝试以下方法:-

// Parent component 
class Parent extends React.Component {
 render(){
  return (
   <View>
    <Child customStyle={styles.childStyle} />
   </View>
  )
}
}

const styles = StyleSheet.create({
 childStyle: {
   color: "red"
 }
});

// Child component

class Child extends React.Component {
  render () {
    return (    
    <View>
        <Text style={[this.props.customStyle, styles.text]}></Text>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  text: {
    fontSize: 14
  }
});
//父组件
类父类扩展了React.Component{
render(){
返回(
)
}
}
const styles=StyleSheet.create({
儿童风格:{
颜色:“红色”
}
});
//子组件
子类扩展了React.Component{
渲染(){
报税表(
)
}
}
const styles=StyleSheet.create({
正文:{
尺寸:14
}
});

您创建了
const.styles
但您正在调用
{classes.field}

尝试将
样式
更改为
或:

const { styles } = this.props
通过道具发送给孩子:

 <Child className={styles.field} />

然后

类子级扩展React.Component{
render(){
const{styles}=this.props
返回(
)
}

className
to property我猜不是最好的选择

这个用例是什么?@HarkiratSaluja这是一个简单的例子来描述这个问题,但是在我的实际项目中,我需要将样式从父组件传递到子组件,以覆盖子组件中样式对象的一些值。@HarkiratSaluja哪个包是Styl在本例中,是否从导入eSheet?@JeremyBrown react native
class Child extends React.Component {
 render(){
  const {styles} = this.props
  return (
   <div>
    <Child className={styles.field} />
   </div>
  )
}