简体   繁体   English

反应:如何“记住”最初的道具

[英]React: how to “remember” initial props

I am playing around with React and Redux and built a simple todo-app. 我正在玩React和Redux,并构建了一个简单的todo-app。 Now, I want to make a save button for user, that gonna save changes made to the todos if there were any (ie changing text). 现在,我想为用户创建一个保存按钮,该按钮将保存对待办事项的更改(如果有的话)(即更改文本)。 So the save button needs to know somehow if there were any changes made to the todos. 因此,保存按钮需要知道是否对待办事项进行了任何更改。

For example, initially we have these todos that are coming from Redux: 例如,最初我们有来自Redux的以下待办事项:

const todos = [
    {
        id: 0,
        text: 'Learn Redux',
    },
    {
        id: 1,
        text: 'Learn React',
    }
]

But the user made some changes to the todos! 但是用户对待办事项做了一些更改!

const todos = [
    {
        id: 0,
        text: 'Eat a cookie',
    },
    {
        id: 1,
        text: 'Read a book',
    }
]

So my question is, how can I detect those changes? 所以我的问题是,我怎么能发现这些变化?

There are many ways you can do this. 您可以通过多种方式执行此操作。 You could use the constructor to remember initial props by doing something like: 您可以通过类似以下方式使用构造函数来记住初始道具:

constructor(props) {
   super(props);
   this.initialProps = props;
}

Since you are using redux you could also handle this in a middleware who's main job is to check and see if the data you are trying to save is different then the value that is kept in the store. 由于您正在使用redux,因此您也可以在中间件中进行处理,该中间件的主要工作是检查并查看要保存的数据是否不同于存储在存储中的值。 Here are the docs for middleware in redux. 是redux中的中间件文档。

You can dispatch an action to notify the save button whenever you dispatch an action changing the todos. 您可以在分派更改待办事项的操作时分派一个操作以通知保存按钮。 Also if your reducer is pure, it will not mutate the previous state, so it is easy to know whether the todos has changed or not. 同样,如果您的减速器是纯色的,则它不会改变先前的状态,因此很容易知道待办事项是否已更改。 if the object reference is different, it is changed. 如果对象引用不同,则将其更改。

Since the point of this is to learn best practices around React + Redux, the best way to do this would be to have your props be the todos coming from your Redux state, and then have your component state be whatever it is currently being mutated to. 因为这样做的目的是学习React + Redux的最佳实践,所以做到这一点的最佳方法是让自己的道具成为来自Redux状态的待办事项,然后让组件状态成为当前正在突变为的状态。 So in your above example, 因此,在上述示例中,

Redux state which feeds to component props is 馈入组件props的Redux状态是

const todos = [
    {
        id: 0,
        text: 'Learn Redux',
    },
    {
        id: 1,
        text: 'Learn React',
    }
]

And then component state is 然后组件状态为

const todos = [
    {
        id: 0,
        text: 'Eat a cookie',
    },
    {
        id: 1,
        text: 'Read a book',
    }
]

In your constructor you would do something like (_ is a library like lodash ): 在构造函数中,您将执行以下操作(_是类似于lodash的库):

constructor(props) {
   super(props);
   this.state = { todos: _.clone(props.todos) };
}

the component would only ever manipulate this.state.todos and then when you are ready to save, you can compare this.state.todos to this.props.todos to see how things have changed. 该组件只会操纵this.state.todos ,然后在准备保存时,可以将this.state.todosthis.props.todos进行比较,以了解情况如何变化。

Then you dispatch the action to save the todos to your redux state (ex: save(this.state.todos) 然后,您分派操作以将save(this.state.todos)到您的redux状态(例如: save(this.state.todos)

Your component should be listening to Redux state via a library like reselect and its props will be updated. 您的组件应该通过诸如reselect类的库监听Redux状态,并且其道具将被更新。

if you are using thunk middleware then you may write an action creater like 如果您使用的是笨拙的中间件,则可以编写一个动作创建器,例如

const getTodos = (getState)=>getState().todos;
const saveAction = newTodos=> async(dispatch, getState)=>{
    const initialTodos = getTodos(getState()); 
    await dispatch('SAVE_ACTION', newTodos);
    const finalTodos = getTodos(getState());
    if(initialTodos !== finalTodos){
           dispatch('UPDATE_TODOS_IN_SERVER', finalTodos);
    }
}

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

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