简体   繁体   中英

what is the correct way to pass data to create react componment

i am us asp.net before and i will pass data in html .i always put the data in tag and the componment require it

ex:

<body> 
<div id="target"></div> 
<script> 
var data="@Html.Raw(JsonConvert.SerializeObject(Model));" </script> 
</body>

JS:

import React from 'react'; 
import ReactDOM from 'react-dom'; 
var data=window.data;
ReactDOM.render(<comp d={data}/>,document.getElementById('target') );

is it the correct way to pass data? or i should use ajax to get the data?

if you are passing data as prop to a child component you can access it by this.props.YourProp

in this example if you are passing data to your componnet as d you can access it by this.props.d

if your app is growing you are going to need handle complex state changes. so i suggest you to use Racket https://github.com/mohebifar/racket and redux to handle your state changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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