javascript React Redux商店
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript React Redux商店相关的知识,希望对你有一定的参考价值。
export const LOAD_COURSES_SUCCESS = 'LOAD_COURSES_SUCCESS';
export const UPDATE_COURSES_SUCCESS = 'UPDATE_COURSES_SUCCESS';
export const CREATE_COURSES_SUCCESS = 'CREATE_COURSES_SUCCESS';
import * as types from './actionTypes';
export function loadCoursesSuccess(courses) {
return { type: types.LOAD_COURSES_SUCCESS, courses };
}
export function updateCourseSuccess(course) {
return { type: types.UPDATE_COURSES_SUCCESS, course };
}
export function createCourseSuccess(course) {
return { type: types.CREATE_COURSES_SUCCESS, course };
}
import {combineReducers} from 'redux';
import courses from './courseReducer';
const rootReducer = combineReducers({
// courses: courses ES6 Shorthand property name
courses
});
export default rootReducer;
import * as types from '../actions/actionTypes';
import initialState from './initialState';
export default function courseReducer(state = initialState.courses, action) {
switch (action.type) {
case types.LOAD_COURSES_SUCCESS:
return action.courses;
case types.CREATE_COURSES_SUCCESS:
return [
...state,
Object.assign({}, action.course)
];
case types.UPDATE_COURSES_SUCCESS:
return [
...state.filter(course => course.id !== action.course.id),
Object.assign({}, action.course)
];
default:
return state;
}
}
export default {
courses: []
};
import React, {PropTypes} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as courseActions from '../../actions/courseActions';
import CourseList from './CourseList';
import {browserHistory} from 'react-router';
class CoursesPage extends React.Component {
constructor(props, context) {
super(props, context);
this.redirectToAddCoursepage = this.redirectToAddCoursepage.bind(this);
}
redirectToAddCoursepage() {
browserHistory.push('/course');
}
render() {
const {courses} = this.props;
return (
<div>
<h1>Courses</h1>
<input type="submit"
value="Add Course"
className="btn btn-primary"
onClick={this.redirectToAddCoursepage} />
<CourseList courses={courses} />
</div>
);
}
}
CoursesPage.propTypes = {
courses: PropTypes.array.isRequired,
actions: PropTypes.object.isRequired
};
function mapStateToProps(state, ownProps) {
return {
courses: state.courses
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(courseActions, dispatch)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(CoursesPage);
import React, {PropTypes} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as courseActions from '../../actions/courseActions';
import CourseForm from './CourseForm';
import toastr from 'toastr';
class ManageCoursePage extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
course: Object.assign({}, this.props.course),
errors: {},
saving: false
};
this.updateCourseState = this.updateCourseState.bind(this);
this.saveCourse = this.saveCourse.bind(this);
}
componentWillReceiveProps(nextProps) {
if (this.props.course.id != nextProps.course.id) {
// Necessary to populate form when existing course is loaded directly
this.setState({ course: Object.assign({}, nextProps.course) });
}
}
updateCourseState(event) {
const field = event.target.name;
let course = Object.assign({}, this.state.course);
course[field] = event.target.value;
return this.setState({ course: course });
}
saveCourse(event) {
event.preventDefault();
this.setState({saving: true});
this.props.actions.saveCourse(this.state.course)
.then(() => this.redirect())
.catch(error => {
toastr.error(error);
this.setState({saving: false});
});
}
redirect() {
this.setState({saving: false});
toastr.success('Course saved');
this.context.router.push('/courses');
}
render() {
return (
<CourseForm
course={this.state.course}
allAuthors={this.props.authors}
onSave={this.saveCourse}
onChange={this.updateCourseState}
errors={this.state.errors}
saving={this.state.saving} />
);
}
}
// Proptypes
ManageCoursePage.propTypes = {
course: PropTypes.object.isRequired,
authors: PropTypes.array.isRequired,
actions: PropTypes.object.isRequired
};
// Pull in React Router context so router is available on this.context.router
ManageCoursePage.contextTypes = {
router: PropTypes.object
};
function getCourseById(courses, id) {
const course = courses.filter(course => course.id == id);
if (course.length) return course[0];
return null;
}
// Maps State to Props
function mapStateToProps(state, ownProps) {
const courseId = ownProps.params.id; // from the path /course/:id
let course = {id: '', watchHref: '', title: '', authorId: '', length: '', category: ''};
if (courseId && state.courses.length > 0) {
course = getCourseById(state.courses, courseId);
}
const authorsFormattedForDropdown = state.authors.map(author => {
return {
value: author.id,
text: author.firstName + ' ' + author.lastName
};
});
return {
course: course,
authors: authorsFormattedForDropdown
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(courseActions, dispatch)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(ManageCoursePage);
import React, {PropTypes} from 'react';
import CourseListRow from './CourseListRow';
const CourseList = ({courses}) => {
return (
<table className="table">
<thead>
<tr>
<th> </th>
<th>Title</th>
<th>Author</th>
<th>Category</th>
<th>Length</th>
</tr>
</thead>
<tbody>
{courses.map(course =>
<CourseListRow key={course.id} course={course}/>
)}
</tbody>
</table>
);
};
CourseList.propTypes = {
courses: PropTypes.array.isRequired
};
export default CourseList;
import React, {PropTypes} from 'react';
import {Link} from 'react-router';
const CourseListRow = ({course}) => {
return (
<tr>
<td><a href={course.watchHref} target="_blank">Watch</a></td>
<td><Link to={'/course/' + course.id}>{course.title}</Link></td>
<td>{course.authorId}</td>
<td>{course.category}</td>
<td>{course.length}</td>
</tr>
);
};
CourseListRow.propTypes = {
course: PropTypes.object.isRequired
};
export default CourseListRow;
以上是关于javascript React Redux商店的主要内容,如果未能解决你的问题,请参考以下文章
如何纠正 Redux 商店中的 React-Redux-Firebase 错误
React Redux - 使用导航菜单设置存储和更新 UI