我知道ReactJs
但我是Redux
的新手。我正在尝试执行异步操作,就像我调用API并希望显示从它接收的数据。当我在单个页面中实现redux
时,即在一个页面上的操作,缩减器,容器工作正常。单页代码如下:
import React from 'react';
import { render } from 'react-dom';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import { connect } from 'react-redux';
function fetchPostsRequest(){
return {
type: "FETCH_REQUEST"
}
}
function fetchPostsSuccess(payload) {
return {
type: "FETCH_SUCCESS",
payload
}
}
function fetchPostsError() {
return {
type: "FETCH_ERROR"
}
}
const reducer = (state = {}, action) => {
switch (action.type) {
case "FETCH_REQUEST":
return state;
case "FETCH_SUCCESS":
return {...state, posts: action.payload};
default:
return state;
}
}
function fetchPostsWithRedux() {
return (dispatch) => {
dispatch(fetchPostsRequest());
return fetchPosts().then(([response, json]) =>{
if(response.status === 200){
dispatch(fetchPostsSuccess(json.data))
}
else{
dispatch(fetchPostsError())
}
})
}
}
function fetchPosts() {
const URL = "api-url";
let user = JSON.parse(localStorage.getItem('user'));
return fetch(URL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
api_token: user.api_token,
type: 'task',
})
}).then(response => Promise.all([response, response.json()]));
}
class App extends React.Component {
componentDidMount(){
this.props.fetchPostsWithRedux()
}
render(){
return (
<ul>
{
this.props.posts &&
this.props.posts.map((post,i) =>{
return(
<li key={i}>{JSON.parse(post.form_data).title}</li>
)
})
}
</ul>
)
}
}
function mapStateToProps(state){
return {
posts: state.posts
}
}
let Container = connect(mapStateToProps, {fetchPostsWithRedux})(App);
const store = createStore(
reducer,
applyMiddleware(thunk)
);
ReactDOM.render(
<Provider store={store}>
<Container/>
</Provider>,
document.getElementById('app')
);
但是当我尝试在不同文件中创建动作,缩减器,容器时,空白页面即将到来。控制台中没有错误,只有三个警告:
Store.js
代码如下:
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import reducer from '../reducers/loginReducer';
const store = createStore(
reducer,
applyMiddleware(thunk)
);
var routes =(
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={Main}>
<Route path="/testing" component={Testing}>
</Router>
</Provider>
);
Testing.js
代码如下:
import React from 'react';
import { render } from 'react-dom';
import ReactDOM from 'react-dom';
import loginContainer from '../containers/loginContainer';
export default class Testing extends React.Component {
render() {
console.log("jgkj");
return (
<loginContainer />
);
}
}
module.exports = Testing;
loginContainer.js
代码如下:
import React, {Component} from 'react';
import { Link } from 'react-router';
import ReactDOM from 'react-dom';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { fetchPostsWithRedux,fetchPosts,fetchPostsError,fetchPostsSuccess,fetchPostsRequest } from '../actions/loginAction';
class loginContainer extends React.Component {
componentDidMount(){
this.props.fetchPostsWithRedux()
}
render(){
return (
<ul>
{
this.props.posts &&
this.props.posts.map((post,i) =>{
return(
<li key={i}>{JSON.parse(post.form_data).title}</li>
)
})
}
</ul>
)
}
}
function mapStateToProps(state){
return {
posts: state.posts
}
}
export default connect(mapStateToProps, {fetchPostsWithRedux})(loginContainer);
loginReducer.js
代码是:
import {
FETCH_REQUEST,
FETCH_SUCCESS,
FETCH_ERROR
} from '../actions/loginAction';
export default function reducer (state = {}, action) {
switch (action.type) {
case "FETCH_REQUEST":
return state;
case "FETCH_SUCCESS":
return {...state, posts: action.payload};
default:
return state;
}
}
loginAction.js
代码如下:
export function fetchPostsRequest(){
return {
type: "FETCH_REQUEST"
}
}
export function fetchPostsSuccess(payload) {
return {
type: "FETCH_SUCCESS",
payload
}
}
export function fetchPostsError() {
return {
type: "FETCH_ERROR"
}
}
export function fetchPostsWithRedux() {
return (dispatch) => {
dispatch(fetchPostsRequest());
return fetchPosts().then(([response, json]) =>{
if(response.status === 200){
dispatch(fetchPostsSuccess(json.data))
}
else{
dispatch(fetchPostsError())
}
})
}
}
export function fetchPosts() {
const URL = "api-url";
let user = JSON.parse(localStorage.getItem('user'));
return fetch(URL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
api_token: user.api_token,
type: 'task',
})
}).then(response => Promise.all([response, response.json()]));
}
答案 0 :(得分:1)
您的类loginContainer名称以小写字母开头,不应该是这种情况,它应该以您的转换程序的大写字母开头,更改为
import {bindActionCreators} from 'redux';
class LoginContainer extends React.Component {
componentDidMount(){
this.props.fetchPostsWithRedux()
}
render(){
return (
<ul>
{
this.props.posts &&
this.props.posts.map((post,i) =>{
return(
<li key={i}>{JSON.parse(post.form_data).title}</li>
)
})
}
</ul>
)
}
}
function mapStateToProps(state){
return {
posts: state.posts
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({fetchPostsWithRedux: fetchPostsWithRedux}, dispatch)
}
export default connect(mapStateToProps,mapDispatchToProps)(LoginContainer);
此外,您还没有使用调度进行操作。利用bindActionCreators
绑定您的行动以发送
同样在 Testing.js 中使用
import LoginContainer from '../containers/loginContainer';
export default class Testing extends React.Component {
render() {
console.log("jgkj");
return (
<LoginContainer />
);
}
}
module.exports = Testing;