2017-03-09 62 views
0

我試圖製作一個動態餅圖,其中當滑塊移動時,動態餅圖的值也發生變化,我製作了組件並呈現它們,但我無法引入setState由於這兩種成分都沒有溝通,做什麼,請提示,這裏是我的代碼:如何在反應中添加setState

<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t <script src="https://npmcdn.com/[email protected]/dist/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script> 
 
<script src="https://unpkg.com/[email protected]/dist/react-with-addons.js"></script> 
 
<script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script> 
 
<script src="https://npmcdn.com/[email protected]/build/public/js/react-d3.js"></script> 
 
     <link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet"> 
 
\t \t 
 

 

 
\t \t 
 
\t </head> 
 
\t <body> 
 
\t \t <div class="container"> 
 
\t \t <h1 class="page-header">Dynamic Pie Chart</h1> 
 
\t \t <div id="slider" class="col-lg-6"></div> 
 
\t \t <div id="container" class="col-lg-6"> 
 
    
 
</div> 
 
</div> 
 
    </div> 
 
\t </body> 
 
\t <script type="text/babel"> 
 
\t 
 
\t 
 

 
class Chart extends React.Component{ 
 
    render() { 
 
    var PieChart = rd3.PieChart; 
 
\t var pieData = [{label: "First", value: 50}, {label: "Second", value: 50 }]; 
 
    return (
 
    \t <PieChart 
 
     data={pieData} 
 
     width={450} 
 
     height={400} 
 
     radius={110} 
 
     sectorBorderColor="white" 
 
     /> 
 
)} 
 
} 
 
Chart.propTypes={ 
 
\t pieData: React.PropTypes.arrayOf(React.PropTypes.shape({ 
 
\t \t \t \t label: React.PropTypes.string.isRequired, 
 
\t \t \t \t value: React.PropTypes.number.isRequired })).isRequired 
 
} 
 
class Slider extends React.Component{ 
 
\t render(){ 
 
\t \t return (<div><input type='range' min='0' max='100' onChange = {this.props.sliderEvent} /></div>); 
 
\t } 
 
} 
 

 
class App extends React.Component{ 
 
\t 
 
\t \t \t constructor(props){ 
 
\t \t \t \t super(props); 
 
\t \t \t \t this.state = { 
 
\t \t \t \t \t pieData : [ 
 
\t \t \t \t \t \t {label: "First", value: 50}, {label: "Second", value: 50 } 
 
\t \t \t \t \t ] 
 
\t \t \t \t }; 
 
\t \t } 
 
\t \t handleSliderEvent(e){ 
 
\t \t \t var slices1 = (360 * e.target.value)/100; 
 
\t \t \t var slices2 = 360 - slices1; 
 
\t \t \t var array1 = [ 
 
\t \t \t \t { label: "First", value: slices1 }, 
 
\t \t \t \t { label: "Second", value: slices2 } 
 
\t \t \t ]; 
 
\t \t \t this.setState({pieData: array1}); 
 
\t \t \t 
 
\t \t } 
 
\t \t render(){ 
 
\t \t return(<div className="row"> 
 
\t \t \t \t <Slider sliderEvent = {this.handleSliderEvent.bind(this)}/> 
 
\t \t \t \t <div className='col-lg-6'> 
 
\t \t \t \t \t <Chart pieData = {this.state.pieData} /> 
 
\t \t \t \t </div> 
 
\t \t \t \t </div>); 
 
\t \t \t } 
 
\t \t } 
 
\t \t ReactDOM.render(<App/>, document.getElementById('container')); 
 

 

 
\t </script> 
 
</html>

回答

2

你需要獲得的道具傳遞給圖表組件

<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t <script src="https://npmcdn.com/[email protected]/dist/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script> 
 
<script src="https://unpkg.com/[email protected]/dist/react-with-addons.js"></script> 
 
<script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script> 
 
<script src="https://npmcdn.com/[email protected]/build/public/js/react-d3.js"></script> 
 
     <link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet"> 
 
\t \t 
 

 

 
\t \t 
 
\t </head> 
 
\t <body> 
 
\t \t <div class="container"> 
 
\t \t <h1 class="page-header">Dynamic Pie Chart</h1> 
 
\t \t <div id="slider" class="col-lg-6"></div> 
 
\t \t <div id="container" class="col-lg-6"> 
 
    
 
</div> 
 
</div> 
 
    </div> 
 
\t </body> 
 
\t <script type="text/babel"> 
 
\t 
 
\t 
 

 
class Chart extends React.Component{ 
 

 
    render() { 
 
    var PieChart = rd3.PieChart; 
 

 
\t var pieData 
 
    if(!this.props.pieData) 
 
     pieData = [{label: "First", value: 50}, {label: "Second", value: 50 }]; 
 
    else 
 
     pieData = this.props.pieData 
 

 
    return (
 
    \t <PieChart 
 
      data={pieData} 
 
      width={450} 
 
      height={400} 
 
      radius={110} 
 
      sectorBorderColor="white"/> 
 
)} 
 
} 
 

 
Chart.propTypes={ 
 
\t pieData: React.PropTypes.arrayOf(React.PropTypes.shape({ 
 
\t \t \t \t label: React.PropTypes.string.isRequired, 
 
\t \t \t \t value: React.PropTypes.number.isRequired })).isRequired 
 
} 
 
class Slider extends React.Component{ 
 
\t render(){ 
 
\t \t return (<div><input type='range' min='0' max='100' onChange = {this.props.sliderEvent} /></div>); 
 
\t } 
 
} 
 

 
class App extends React.Component{ 
 
\t 
 
\t \t \t constructor(props){ 
 
\t \t \t \t super(props); 
 
\t \t \t \t this.state = { 
 
\t \t \t \t \t pieData : [ 
 
\t \t \t \t \t \t {label: "First", value: 50}, {label: "Second", value: 50 } 
 
\t \t \t \t \t ] 
 
\t \t \t \t }; 
 
\t \t } 
 
\t \t handleSliderEvent(e){ 
 
\t \t \t var slices1 = (360 * e.target.value)/100; 
 
\t \t \t var slices2 = 360 - slices1; 
 
\t \t \t var array1 = [ 
 
\t \t \t \t { label: "First", value: slices1 }, 
 
\t \t \t \t { label: "Second", value: slices2 } 
 
\t \t \t ]; 
 
\t \t \t this.setState(function(prevState) { 
 
       return {pieData: array1} 
 
      }); 
 
\t \t } 
 
\t \t render(){ 
 

 
    \t \t return(<div className="row"> 
 
    \t \t \t \t <Slider sliderEvent = {this.handleSliderEvent.bind(this)}/> 
 
    \t \t \t \t <div className='col-lg-6'> 
 
    \t \t \t \t \t <Chart pieData={this.state.pieData} /> 
 
    \t \t \t \t </div> 
 
    \t \t \t \t </div>); 
 
    \t \t \t } 
 
    \t } 
 
     
 
\t \t ReactDOM.render(<App/>, document.getElementById('container')); 
 

 

 
\t </script> 
 
</html>

0

我已經解決了我自己的問題,在這裏我也做它的方式:

<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t \t <title>Dynamic Pie Chart</title> 
 
\t \t <script src="https://npmcdn.com/[email protected]/dist/react.min.js"></script> 
 
\t \t <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.js"></script> 
 
\t \t <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script> 
 
\t \t <script src="https://unpkg.com/[email protected]/dist/react-with-addons.js"></script> 
 
\t \t <script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script> 
 
\t \t <script src="https://npmcdn.com/[email protected]/build/public/js/react-d3.js"></script> 
 
     <link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet"> 
 
\t </head> 
 
\t <body> 
 
\t \t <div class="container"> 
 
\t \t \t <h1 class="page-header">Dynamic Pie Chart</h1> 
 
\t \t \t <div id="container" class="col-lg-12"></div> 
 
\t \t </div> 
 
\t </body> 
 
\t <script type="text/babel"> 
 
\t \t class Chart extends React.Component{ 
 
\t \t \t render() { 
 
\t \t \t \t var PieChart = rd3.PieChart; 
 
\t \t \t \t var pieData = [{label: "First", value: 50}, {label: "Second", value: 50 }]; 
 
\t \t \t \t return (
 
\t \t \t \t \t <PieChart 
 
\t \t \t \t \t \t data={this.props.pieData} 
 
\t \t \t \t \t \t width={450} 
 
\t \t \t \t \t \t height={400} 
 
\t \t \t \t \t \t radius={110} 
 
\t \t \t \t \t \t sectorBorderColor="white" 
 
\t \t \t \t \t /> 
 
\t \t \t \t)} 
 
\t \t } 
 
\t \t Chart.propTypes={ 
 
\t \t \t \t pieData: React.PropTypes.arrayOf(React.PropTypes.shape({ 
 
\t \t \t \t label: React.PropTypes.string.isRequired, 
 
\t \t \t \t value: React.PropTypes.number.isRequired })).isRequired 
 
\t \t } \t 
 
\t \t class Slider extends React.Component{ 
 
\t \t \t render(){ 
 
\t \t \t \t return (<div className='col-lg-6'><input type='range' min='0' max='100' onChange = {this.props.sliderEvent} /></div>); 
 
\t \t \t } 
 
\t \t } 
 
\t \t class App extends React.Component{ 
 
\t \t \t constructor(props){ 
 
\t \t \t \t super(props); 
 
\t \t \t \t this.state = { 
 
\t \t \t \t \t pieData : [{label: "First", value: 50}, {label: "Second", value: 50 }] 
 
\t \t \t \t }; 
 
\t \t \t } 
 
\t \t \t handleSliderEvent(e){ 
 
\t \t \t \t var slices1 = (100 * e.target.value)/100; 
 
\t \t \t \t var slices2 = 100 - slices1; 
 
\t \t \t \t var array1 = [ 
 
\t \t \t \t \t { label: "First", value: slices1 }, 
 
\t \t \t \t \t { label: "Second", value: slices2 } 
 
\t \t \t \t ]; 
 
\t \t \t \t this.setState({pieData: array1}); 
 
\t \t \t \t console.log('slice 1:'+slices1); 
 
\t \t \t \t console.log('slice 2:'+slices2) 
 
\t \t \t } 
 
\t \t \t render(){ 
 
\t \t \t \t return(<div> 
 
\t \t \t \t \t \t \t <Slider sliderEvent = {this.handleSliderEvent.bind(this)}/> 
 
\t \t \t \t \t \t \t <div className='col-lg-6'> 
 
\t \t \t \t \t \t \t \t <Chart pieData = {this.state.pieData} /> 
 
\t \t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t </div>); 
 
\t \t \t } 
 
\t \t } 
 
\t \t ReactDOM.render(<App/>, document.getElementById('container')); 
 
\t </script> 
 
</html>