[英]Update state when data changed in React+Redux
I'm too new to react. 我太新了,无法做出反应。 I'm curious about a few things. 我对几件事感到好奇。 The first topic I was wondering, why the HTML tags in js? 我想知道的第一个主题是为什么js中的HTML标签?
I need to do a project. 我需要做一个项目。 I have a method that returns json with .NET I have a code like below. 我有一个用.NET返回json的方法,我有下面的代码。 How do I update the div when I add something into it? 在其中添加内容时,如何更新div?
This .net code. 此.net代码。
private static readonly IList<Mock.Model.Skills> _Skills;
[Route("api/Skills/add")]
[HttpPost]
public ActionResult Skills(Mock.Model.Skills Model)
{
if (ModelState.IsValid)
{
_Skills.Add(Model);
return Json("true");
}
return Json(false);
}
And, js (react) code. 并且,js(反应)代码。
var Rows = React.createClass({
render: function () {
return (
<span className="label label-info tags">{this.props.item.tag}</span>
);
}
});
var SkillsRow = React.createClass({
getInitialState: function () {
return {
items: []
}
},
componentDidMount: function () {
$.get(this.props.dataUrl, function (data) {
if (this.isMounted()) {
this.setState({
items: data
});
}
}.bind(this));
},
render: function () {
var rows = [];
this.state.items.forEach(function (item, index) {
rows.push(<Rows class="label label-info tags" key={index} item={item} />);
});
return (<span>{rows}</span>);
}
});
ReactDOM.render(
<SkillsRow dataUrl="/api/Skills" />,
document.getElementById('skills-data')
);
This works well, but do not add. 这很好用,但不要添加。 I wonder if this is the correct method? 我想知道这是否是正确的方法吗?
Thank you to everyone who showed interest. 谢谢所有对此感兴趣的人。
If you want to add items to api, you can call something like this: 如果要将项目添加到api,则可以调用类似以下内容的代码:
var SkillsRow = React.createClass({
getInitialState: function() {
return {
items: [],
currentEditor: ''
}
},
componentDidMount: function() {
this.updateSkillList()
},
updateSkillList: function() {
$.get(this.props.dataUrl, function(data) {
this.setState({
items: data
})
}).bind(this)
},
handleEditorChange: function(event) {
this.setState({
currentEditor: event.target.value
})
},
handlePostForm: function() {
$.post('api/Skills/add', {skill: this.state.currentEditor}, function(data) {
this.updateSkillList()
})
},
renderSkillList: function() {
this.state.items.map(function(item, idx) {
return <Row className="label label-info tags" key={idx} item={item} />
})
},
render: function() {
return (
<div>
<span>
<input value={this.state.currentEditor} onChange={this.handleEditorChange} />
<button onClick={this.handlePostForm} />
</span>
<span>{this.renderSkillList()}</span>
</div>
)
}
})
Edited: Now i understood the question, you code will look something like this, also you will have to fix backend code to only receive the skill name, and then create the object (you can't create a C# object in javascript) 编辑:现在我明白了这个问题,您的代码将看起来像这样,也将必须修复后端代码以仅接收技能名称,然后创建对象(您无法在javascript中创建C#对象)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.