简体   繁体   English

使用 React 增加 Material Design Lite 进度条

[英]Incrementing the Material Design Lite Progress bar with React

I've got MDL running with React at the moment and it seems to be working fine at the moment.我现在已经让MDL与 React 一起运行,目前它似乎运行良好。

I've got the Progress Bar appearing on the page as needed and it loads up with the specified 'progress' on page load when either entering in a number directly:我已经根据需要在页面上显示了进度条,当直接输入数字时,它会在页面加载时加载指定的“进度”:

document.querySelector('#questionnaireProgressBar').addEventListener('mdl-componentupgraded', function() {
    this.MaterialProgress.setProgress(10);
})

or when passing in a number via a Variable:或者通过变量传递数字时:

document.querySelector('#questionnaireProgressBar').addEventListener('mdl-componentupgraded', function() {
    this.MaterialProgress.setProgress(value);
})

It stops working after this though.在此之后它停止工作。 I try to update the value via the Variable and it doesn't update.我尝试通过变量更新值,但它没有更新。 I've been advised to use this:我被建议使用这个:

document.querySelector('.mdl-js-progress').MaterialProgress.setProgress(45);

to update the value but it doesn't work.更新值,但它不起作用。 Even when trying it directly in the console.即使直接在控制台中尝试。

When trying via the Console I get the following Error:通过控制台尝试时出现以下错误:

Uncaught TypeError: document.querySelector(...).MaterialProgress.setProgress is not a function(…)

When I try to increment the value via the Variable I get no errors and when I console.log(value) I am presented the correct number (1,2,3,4...) after each click event that fires the function (it fires when an answer is chosen in a questionnaire)当我尝试通过变量增加值时,我没有收到任何错误,当我使用 console.log(value) 时,在每次触发函数的点击事件后,我都会看到正确的数字(1,2,3,4 ...)(在问卷中选择答案时触发)

What I want to know is if there's something obvious that I'm missing when using MTL and React to make components to work?我想知道的是,在使用 MTL 和 React 使组件工作时,是否有明显的遗漏? There was an issue with scope but I seem to have it fixed with the following:范围存在问题,但我似乎已通过以下方式修复它:

updateProgressBar: function(value) {
    // fixes scope in side function below
    var _this = this;

    document.querySelector('#questionnaireProgressBar').addEventListener('mdl-componentupgraded', function() {
        this.MaterialProgress.setProgress(value);
    })
},

In React I've got the parent feeding the child with the data via props and I'm using "componentWillReceiveProps" to call the function that updates the progress bar.在 React 中,我让父母通过道具向孩子提供数据,我正在使用“componentWillReceiveProps”来调用更新进度条的函数。

I've used the "componentDidMount" function too to see if it makes a difference but it still only works on page load.我也使用了“componentDidMount”函数来查看它是否有所作为,但它仍然只适用于页面加载。 From what I've read, it seems that I should be using "componentWillReceiveProps" over "componentDidMount".从我读过的内容来看,似乎我应该在“componentDidMount”上使用“componentWillReceiveProps”。

It's being fed from the parent due to components sending data between each other.由于组件在彼此之间发送数据,它是由父级提供的。 I've used their doc's and some internet help to correctly update the parent function to then update the progress bar in the separate component.我已经使用他们的文档和一些互联网帮助来正确更新父函数,然后更新单独组件中的进度条。

updateProgressBarTotal: function(questionsAnsweredTotal) {
    this.props.updateProgressBarValue(questionsAnsweredTotal);
}

The parent function looks like the following (I think this may be the culprit):父函数如下所示(我认为这可能是罪魁祸首):

// this is passed down to the Questions component
updateProgressBarTotal: function(questionsAnsweredTotal) {
    this.setState({
        progressBarQuestionsAnswered : questionsAnsweredTotal
    })
}

I can post up some more of the code if needed.如果需要,我可以发布更多代码。

Thank you谢谢

Looks I needed a fresh set of eyes on this.看起来我需要对此有新的看法。

I moved the function to the child of the parent.我把这个功能移到了父母的孩子身上。 It seems that using document.querySelector... when in the parent doesn't find the element but when it's moved to the child where I do all the question logic it seems to be fine.似乎使用document.querySelector...当在父级中找不到该元素时,但是当它被移动到我执行所有问题逻辑的子级时,它似乎没问题。 It increments the progress correctly etc now:)它现在正确地增加了进度等等:)

// goes to Questionnaire.jsx (parent) to update the props
updateProgressBarTotal: function(questionsAnsweredTotal) {
    // updates the state in the parent props
    this.props.updateProgressBarValue(questionsAnsweredTotal);
    // update the progress bar with Value from questionsAnsweredTotal
    document.querySelector('.mdl-js-progress').MaterialProgress.setProgress(questionsAnsweredTotal);
},

I had same problem in angular2 application.我在 angular2 应用程序中遇到了同样的问题。 You don't necessary need to move to the child component.您不必移动到子组件。

I found after struggling to find a reasonable fix that you simply have to be sure mdl-componentupgraded event already occurred before being able to use MaterialProgress.setProgress(VALUE) .在努力寻找合理的解决方案后,我发现您只需确保mdl-componentupgraded事件已经发生,然后才能使用MaterialProgress.setProgress(VALUE) Then it can be updated with dynamic value.然后它可以用动态值更新。

That is why moving to the child works.这就是为什么转移到孩子身上有效的原因。 In the parent component mdl-componentupgraded event had time to occur before you update progress value在更新进度值之前,父组件中的mdl-componentupgraded事件有时间发生

My solution for angular2 in this article我在本文中针对angular2的解决方案

Adapted in a React JS application:在 React JS 应用程序中改编:

in componentDidMount , place a flag mdlProgressInitDone ( initiated to false ) in mdl-componentupgraded callback:componentDidMount中,在mdl-componentupgraded回调中放置一个标志mdlProgressInitDone初始化为 false ):

// this.ProgBar/nativeElement 
// is angular2 = document.querySelector('.mdl-js-progress')
var self = this;
this.ProgBar.nativeElement.addEventListener('mdl-componentupgraded', function() {
  this.MaterialProgress.setProgress(0);
  self.mdlProgressInitDone = true; //flag to keep in state for exemple
});

Then in componentWillReceiveProps test the flag before trying to update progress value:然后在componentWillReceiveProps尝试更新进度值之前测试标志:

this.mdlProgressInitDone ? this.updateProgress() : false;

updateProgress() {
this.ProgBar.nativeElement.MaterialProgress.setProgress(this.currentProgress);  
}

After attaching the progress bar to the document, execute:将进度条附加到文档后,执行:

function updateProgress(id) {
    var e = document.querySelector(id);
    componentHandler.upgradeElement(e);
    e.MaterialProgress.setProgress(10);
}

updateProgress('#questionnaireProgressBar');

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

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