Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.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
Javascript reactjs+;有状态组件的样式化组件-我的样式可以';我看不到道具_Javascript_Reactjs_Styled Components - Fatal编程技术网

Javascript reactjs+;有状态组件的样式化组件-我的样式可以';我看不到道具

Javascript reactjs+;有状态组件的样式化组件-我的样式可以';我看不到道具,javascript,reactjs,styled-components,Javascript,Reactjs,Styled Components,为什么在使用类/有状态组件时看不到道具: const StyledTitle = styled.h1 ` color: ${props => (props.primary ? "royalblue" : "black")}; `; class Title extends Component { render() { return <StyledTitle > { this.props.children } < /StyledTitle

为什么在使用类/有状态组件时看不到道具:

const StyledTitle = styled.h1 `
  color: ${props => (props.primary ? "royalblue" : "black")};
`;

class Title extends Component {
  render() {
    return <StyledTitle > {
      this.props.children
    } < /StyledTitle>;
  }
}

const App = () => ( 
  <div>
    <Title>Hi, Alice!</Title>
    <Title primary>Hi Bob, you are awesome!</Title>
  </div>
);
const StyledTitle=styled.h1`
颜色:${props=>(props.primary?“皇家蓝”:“黑色”)};
`;
类标题扩展组件{
render(){
返回{
这是道具
};
}
}
常量应用=()=>(
嗨,爱丽丝!
嗨,鲍勃,你真棒!
);
以下是样式化组件的示例:


演示:

您没有将
primary
属性传递给样式化组件,因此它可以根据
primary
属性呈现逻辑。只需将其添加到组件声明中

const StyledTitle = styled.h1 `
    color: ${props => (props.primary ? "royalblue" : "black")};
`;

class Title extends Component {
  render() {
    return <StyledTitle primary={this.props.primary}> {
      this.props.children
    } < /StyledTitle>;
  }
}

const App = () => ( 
  <div>
    <Title>Hi, Alice!</Title>
    <Title primary>Hi Bob, you are awesome!</Title>
  </div>
);
const StyledTitle=styled.h1`
颜色:${props=>(props.primary?“皇家蓝”:“黑色”)};
`;
类标题扩展组件{
render(){
返回{
这是道具
};
}
}
常量应用=()=>(
嗨,爱丽丝!
嗨,鲍勃,你真棒!
);

您没有将
primary
属性传递给样式化组件,因此它可以根据
primary
属性呈现逻辑。只需将其添加到组件声明中

const StyledTitle = styled.h1 `
    color: ${props => (props.primary ? "royalblue" : "black")};
`;

class Title extends Component {
  render() {
    return <StyledTitle primary={this.props.primary}> {
      this.props.children
    } < /StyledTitle>;
  }
}

const App = () => ( 
  <div>
    <Title>Hi, Alice!</Title>
    <Title primary>Hi Bob, you are awesome!</Title>
  </div>
);
const StyledTitle=styled.h1`
颜色:${props=>(props.primary?“皇家蓝”:“黑色”)};
`;
类标题扩展组件{
render(){
返回{
这是道具
};
}
}
常量应用=()=>(
嗨,爱丽丝!
嗨,鲍勃,你真棒!
);