简体   繁体   English

添加内联样式以做出反应

[英]Adding inline styles to react

Currently I'm using react.js and I'm trying to get two div's to be side by side.目前我正在使用 react.js 并且我试图让两个 div 并排。 Currently I'm trying to目前我正在尝试

<div id="sidebar" style = "display:inline-block;" >
  <script src="build/sidebar.js"></script>
</div>
<div id="map-canvas" style="display: inline-block; width: 20%; height: 50%; "></div>

with sidebar.js as the where react is stored.使用 sidebar.js 作为存储反应的地方。 This unfortunately doesnt work however as it just moves map-canvas to the side while sidebar isn't to the left of it;不幸的是,这不起作用,因为它只是将地图画布移到一边,而侧边栏不在它的左侧; its on top.它在上面。 I've tried many various combinations w/ float as well and none of them seem to work我也尝试了许多带有浮动的各种组合,但似乎没有一个有效

Another option is to edit the sidebar.js code where I currently have另一种选择是编辑我目前拥有的 sidebar.js 代码

return <div>
  <input type="text" value={this.state.searchString} onChange={this.handleChange} placeholder="Type here" />
  <ul>
    { libraries.map(function(l){
      return <li>{l.name} </li>
    }) }
  </ul>
</div>;

in which I try doing return <div style ="display: inline-block;"> However, in this case the generated html doesnt show up at all.其中我尝试做return <div style ="display: inline-block;">但是,在这种情况下,生成的 html 根本不显示。 I'm perplex to what I should try but react seems like it doesnt want to play nice with other div elements.我对我应该尝试什么感到困惑,但 React 似乎不想与其他 div 元素一起玩。

That's because in React, the style prop takes an object instead of a semicolon-separated string .那是因为在 React 中, style prop接受一个对象而不是分号分隔的 string

<div id="sidebar" style={{display : 'inline-block'}} >
  <script src="build/sidebar.js"></script>
</div>
<div id="map-canvas" style={{display: 'inline-block', width: '20%', height: '50%'}}>
</div>

Edit:编辑:

So this:所以这:

return <div style="display: inline-block;">

Would become this:会变成这样:

return <div style={{display: 'inline-block'}}>
const styles = {
  container: {
   backgroundColor: '#ff9900',
   ...
   ...
   textAlign: 'center'
  }
}

export default class App extends React.Component {
  render() {
    return(
      <div className="container" style={styles.container}>
      // your other codes here...
      </div>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM