简体   繁体   English

如何在React js中插入json数据?

[英]How to insert json data in React js?

I have a following list of data's. 我有以下数据列表。 I need to insert my template. 我需要插入我的模板。 How to achieve this one with React js. 如何使用React js实现这一目标。

[
 {
  product:"one",
  quantiy:2
 },
 {
  product:"two",
  quantiy:4
 },
 {
  product:"three",
  quantiy:3
 }
]

Here is working code: http://jsbin.com/wafemul/edit?html,js,output 这是工作代码: http//jsbin.com/wafemul/edit?html,js,output

var data = [
 {
   product:"one",
   quantiy:2
 },
 {
   product:"two",
   quantiy:4
  },
 {
  product:"three",
  quantiy:3
 }
];

class App extends React.Component {

 constructor(props){
  super(props);
   this.state= {
    data: data
   }
  }

render() {
  return (
    <div>
      <h1>Heading</h1>
      <div>{this.state.data.map( item => (
       <li>{item.product}</li>
       ))}
      </div>
     </div>
    );
  }
};

ReactDOM.render(
 <App />,
 document.getElementById('box')
);

Pass it as a propety to the component and you can access it using this.props.data in the render method. 将它作为组件传递给组件,您可以使用render方法中的this.props.data访问它。

Ex: 例如:

 var data = [....] <SampleComponent data={data} />, 

Or use state to pass data 或使用状态传递数据

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

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