简体   繁体   English

使用chart.js时在react.js中使用props传递值的问题

[英]Problem in passing values using props in react.js while using chart.js

I am using react js and trying to send Props from my app.js to chart.js file.我正在使用 react js 并尝试将道具从我的 app.js 发送到 chart.js 文件。 When I send the hardcoded values the values are being send properly and graph is made according to it.当我发送硬编码值时,值被正确发送并根据它制作图形。 But whenever I am passing a dynamic values, values are being passed but not used by chart.但是每当我传递动态值时,都会传递值但图表未使用这些值。

App.js应用程序.js

 class App extends React.Component {

  state = {
    text: "",
    credit: [{_id:1,financeCredit:"loading"}],
    debit: [{_id:1,financeDebit:"loading"}],
   }

componentDidMount(){
    fetch('/data')
    .then(res=> res.json())
    .then(res2 => {
      console.log(res2)
      this.setState({
        credit: res2
      })
    })

    fetch('/data2')
    .then(res=> res.json())
    .then(res2 => {
      console.log(res2)
      this.setState({
        debit: res2
      })
    })

}

  render(){

var lengthExpense = this.state.credit.length;
console.log(lengthExpense)

var expName = [];

for (var a = 0 ; a <= lengthExpense ; a++){

if (this.state.credit[a]){
    expName.push(this.state.credit[a].expenseName)
}

}

var expAmount = [];

for (var b = 0 ; b <= lengthExpense ; b++){
    if(this.state.credit[b])
    expAmount.push(this.state.credit[b].expenseAmount)
}

console.log(expAmount)
console.log(expName)


    return (
      <BrowserRouter>
      <div className="App">   
      <Navbar />
      <Chart  expam = {expAmount} expnam = {expName} />
      <Route exact path = "/" component = {Home} />
      </div>
      </BrowserRouter>
  );
}

}

export default App;

Although the following console.log() is showing the desired values I want to pass尽管以下 console.log() 显示了我想传递的所需值

console.log(expAmount)
console.log(expName)

I am passing these values like this我像这样传递这些值

<Chart  expam = {expAmount} expnam = {expName} />

In chart.js although I am getting these values.在chart.js 中,虽然我得到了这些值。

Chart.js图表.js

class Chart extends Component{

    constructor(props){
        super(props);
        this.state = {
            chartData : {
                labels: this.props.expnam,
                datasets: [{
                    label: 'Expense',
                    data: this.props.expam,
                    backgroundColor:'rgba(255, 99, 132, 0.6)'
                }]
            }
        }
    }

    render(){

console.log(this.props)

        return(
            <div className = "chart">
                <div>
            <Bar id = "chart1"
            data={this.state.chartData}
            options={{ maintainAspectRatio: false, }}
            />  

            <canvas id = "chart1" height="30vw" width="10vw" ></canvas>
                </div>

            </div>
        )
    }
}

But couldn't able to pass it to to labels and data.但无法将其传递给标签和数据。 Code is properly running but there is no values so chart is being showed empty代码正常运行但没有值所以图表显示为空

constructor(props){
        super(props);
        this.state = {
            chartData : {
                labels: this.props.expnam,
                datasets: [{
                    label: 'Expense',
                    data: this.props.expam,
                    backgroundColor:'rgba(255, 99, 132, 0.6)'
                }]
            }
        }
    }

In this chart.js file I can see all the values that are being passed from App.js.在这个 chart.js 文件中,我可以看到从 App.js 传递的所有值。 But these values are not been used for chart (bar chart).但这些值并未用于图表(条形图)。

console.log(this.props)

It seems like you're setting the data in your constructor:似乎您正在构造函数中设置数据:

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

A constructor is only called once when an object is initialized.构造函数仅在对象初始化时调用一次 (In ReactJS, it is only called after the first render.) (在 ReactJS 中,它仅在第一次渲染后调用。)

You are calling setState() , and the rest appears to be good, that I can tell.您正在调用setState() ,其余的似乎很好,我可以说。 Why don't you move this.state = {...} from the constructor to render() ?为什么不将this.state = {...}从构造函数移动到render() Since render() runs whenever the state is changed, your setState() calls should work.由于render()在状态更改时运行,因此您的setState()调用应该可以工作。

It may be inelegant, and there can certainly be improvements, but it will get you started in the right direction.它可能不优雅,当然可以改进,但它会让你朝着正确的方向开始。

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

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