I worked on a React application and got to the point where I need Redux to handle some of its aspects.
After reading a bunch of tutorials, I'm pretty obsessed with how to make my smart components dumb and move functions to my actions and gears.
So, for example, one aspect of the application is more of a to-do list style.
One of my classes starts as follows:
export default class ItemList extends React.Component {
constructor() {
super();
this.state = { items: [],
completed: [],
};
this.addItem = this.addItem.bind(this);
this.completeItem = this.completeItem.bind(this);
this.deleteItem = this.deleteItem.bind(this);
}
addItem(e) {
var i = this.state.items;
i.push({
text: this._inputElement.value,
paused: false,
key: Date.now()
});
this.setState({ items: i });
e.preventDefault();
this._inputElement.value = '';
this._inputElement.focus();
}
completeItem(e) {
this.deleteItem(e);
var c = this.state.completed;
c.push({
text: e.target.parentNode.parentNode.getElementsByClassName('item-name')[0].innerHTML,
paused: false,
key: Date.now()
});
this.setState({ completed: c });
}
deleteItem(e) {
var i = this.state.items;
var result = i.filter(function(obj) {
return obj.text !== e.target.parentNode.parentNode.getElementsByClassName('item-name')[0].innerHTML;
});
this.setState({ items: result });
}
render() {
var listItems = this.state.items.map(this.createTasks);
return <div className="item-list">
<form className="form" onSubmit={this.addItem}>
<input ref={(a) => this._inputElement = a}
placeholder="Add new item"
autoFocus />
<button type="submit"></button>
</form>
{listItems}
</div>;
}
}
So, as you can see, this is very logical-heavy. I started adding Redux by adding <Provider>
to my index file and made a basic reducers file, which is still pretty empty:
import { combineReducers } from 'redux';
const itemList = (state = {}, action) => {
};
const rootReducer = combineReducers({
itemList,
});
export default rootReducer;
... and I made an action file that does not yet have much in it.
I'm struggling to figure out:
- , , - JSON, , JSON, ?
- , ? , , , ?