简体   繁体   English

当数据在React + Redux中更改时更新状态

[英]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.

相关问题 react + redux:存储状态已更改,而视图未更改 - react+redux: state of store changed without view changing 在React + Redux中路由状态名称 - Route state name in React+Redux 如何使 state 在 React+Redux 中异步更新 - How can I make state updating asynchronous in React+Redux 有没有一种方法可以使用React + Redux在URL查询中存储部分应用程序状态? - Is there a way to store part of application state in URL query with React+Redux? 何时基于来自redux的数据更新本地反应状态? - When to update local react state based on data from redux? REACT - REDUX 如何更新子组件中 redux 应用程序的全局状态,子组件在安装应用程序时在父组件中获取数据 - REACT - REDUX how to update the global state of a redux app in child component that fetched data in parent when app is mounted 如何在React-Redux中更改状态时关闭模态对话框? - How to close the Modal Dialog when state changed in React-Redux? React + Redux-期望减速器具有功能 - React+Redux - Expected the reducer to be a function 通过此内部对象的引用react + redux - Pass reference of this inside object react+redux 当我们不知道数据的嵌套级别时,如何在react / redux中的reducer state属性中更新数据? - How to update a data in a reducer state property in react/redux, when we don't know the level of nesting for the data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM