使用 react-routerv4 在 react-redux 应用程序中从状态渲染组件时出现 404 错误?
404 error when rendering component from state in react-redux app with react-routerv4?
*这是一个 React-Redux 教程,所以请不要使用其他解决方案:)
期望的行为:将父组件的属性从 redux 控制的状态传递给 PostDetail 组件
当前输出:
(1) 204 结果:
http://localhost:3001/react/8xf0y6ziyjabvozdd253nd
Request Method: OPTIONS
**strong text**Status Code: 204 No Content
(2) 控制台错误:
GET http://localhost:3001/react/8xf0y6ziyjabvozdd253nd 404 (Not Found)
:3000/react/8xf0y6ziyjabvozdd253nd:1
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
在我的应用程序 (root.js) 文件中,我有一个 PostDetail 组件,我想像这样路由到它:
import React, { Component } from 'react';
import { Route } from 'react-router-dom'
import Categories from './Categories'
import ListPosts from './ListPosts'
import Header from './Header'
import PostDetail from './PostDetail'
class App extends Component {
render() {
return (
<div>
<Header />
<Categories />
<ListPosts />
<Route exact path="/:category/:id" component={PostDetail}/>
</div>
然后在 PostDetail 嵌套组件中(在 ListPosts 组件内)我在 material-ui 平面按钮中有一个函数,(重要旁注:PostDetail 组件确实有正在调用的 console.log 语句,但 props.post 未定义):
selectPost = (selectedCategory, selectedPost) => {
console.log('selectedCategory', selectedCategory, selectedPost)
this.props.getSpecificPost(selectedCategory, selectedPost);
}
return (
<CardActions>
<FlatButton
label="See Thread"
// href={`/${post.category}/${post.id}`}
containerElement={<Link to={`/${post.category}/${post.id}`} post={this.props.post} />}
onClick={() => this.selectPost(post.category, post.id)}
/>
)
我的 mapStateToProps 和 mapDispatchToProps 在同一个组件中:
const mapStateToProps = state => ({
posts: state.postsReducer.posts,
loading: state.postsReducer.loading,
error: state.postsReducer.error,
selectedCategory: state.categoriesReducer.selectedCategory,
selectedPost: state.postsReducer.selectedPost,
});
function mapDispatchToProps (dispatch) {
return {
getSpecificPost: (selectedCategory, selectedPost) =>
dispatch(getSpecificPost(selectedCategory, selectedPost)),
fetchPosts: (selectedCategory) => dispatch(fetchPosts(selectedCategory))
}
}
export default withRouter(connect(
mapStateToProps,
mapDispatchToProps
)(PostPreview))
我正在使用调用我的 api:
的 redux-thunk 操作调用
export function getSpecificPost(selectedCategory, selectedPost) {
console.log('getSpecificPost', selectedPost)
return dispatch => {
dispatch(beginFetchPosts());
return fetch(selectedPost ? `${api}/${selectedCategory}/${selectedPost}`
: null, { headers })
.then(
res => res.json(),
error => console.log('An error occured at getSpecificPost', error)
)
.then(json => {
dispatch(selectPostSuccess(json));
return json
})
}
}
还有一个减速器:
const initialState = {
posts: [],
categories: [],
loading: false,
error: null,
selectedCategory: 'all',
selectedPost: [],
};
export function postsReducer(state = readableInitialState, action) {
switch(action.type) {
case C.SELECTED_POST:
return {
...state,
loading: false,
selectedPost: action.payload.selectedPost
};
...是的,我用默认语句完成了 switch 语句,据我所知没有语法问题)
每当我看到错误时
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
我回想起第一次设置项目并访问远程服务器进行开发时必须解决的 CORS 问题。
从 chrome 开发工具转到此页面时,您能否提供网络响应? JSON 的开头不应该有 <
,所以我认为您没有反应路由器问题,而是某种服务器配置问题。
问题是你没有提到方法,是GET
,PUT
,所以服务器正在考虑OPTIONS
。您可以以不同的方式使用 fetch
调用。
export function getSpecificPost(selectedCategory, selectedPost){
return (dispatch) => {
const baseURL = selectedPost ? `${api}/${selectedCategory}/${selectedPost}` : null
fetch(baseURL, {
method: 'GET',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(`${headers}`)
})
.then(json => {
dispatch(selectPostSuccess(json))
})
}
}
您还可以通过以下代码片段将数据发送到减速器而不是 json。
export function getSpecificPost(selectedCategory, selectedPost){
return (dispatch) => {
const baseURL = selectedPost ? `${api}/${selectedCategory}/${selectedPost}` : null
fetch(baseURL, {
method: 'GET',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(`${headers}`)
})
.then(json => response.json())
.then(data => {
dispatch(selectPostSuccess(data))
})
}
}
希望对您有所帮助。
*这是一个 React-Redux 教程,所以请不要使用其他解决方案:)
期望的行为:将父组件的属性从 redux 控制的状态传递给 PostDetail 组件
当前输出:
(1) 204 结果:
http://localhost:3001/react/8xf0y6ziyjabvozdd253nd
Request Method: OPTIONS
**strong text**Status Code: 204 No Content
(2) 控制台错误:
GET http://localhost:3001/react/8xf0y6ziyjabvozdd253nd 404 (Not Found)
:3000/react/8xf0y6ziyjabvozdd253nd:1
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
在我的应用程序 (root.js) 文件中,我有一个 PostDetail 组件,我想像这样路由到它:
import React, { Component } from 'react';
import { Route } from 'react-router-dom'
import Categories from './Categories'
import ListPosts from './ListPosts'
import Header from './Header'
import PostDetail from './PostDetail'
class App extends Component {
render() {
return (
<div>
<Header />
<Categories />
<ListPosts />
<Route exact path="/:category/:id" component={PostDetail}/>
</div>
然后在 PostDetail 嵌套组件中(在 ListPosts 组件内)我在 material-ui 平面按钮中有一个函数,(重要旁注:PostDetail 组件确实有正在调用的 console.log 语句,但 props.post 未定义):
selectPost = (selectedCategory, selectedPost) => {
console.log('selectedCategory', selectedCategory, selectedPost)
this.props.getSpecificPost(selectedCategory, selectedPost);
}
return (
<CardActions>
<FlatButton
label="See Thread"
// href={`/${post.category}/${post.id}`}
containerElement={<Link to={`/${post.category}/${post.id}`} post={this.props.post} />}
onClick={() => this.selectPost(post.category, post.id)}
/>
)
我的 mapStateToProps 和 mapDispatchToProps 在同一个组件中:
const mapStateToProps = state => ({
posts: state.postsReducer.posts,
loading: state.postsReducer.loading,
error: state.postsReducer.error,
selectedCategory: state.categoriesReducer.selectedCategory,
selectedPost: state.postsReducer.selectedPost,
});
function mapDispatchToProps (dispatch) {
return {
getSpecificPost: (selectedCategory, selectedPost) =>
dispatch(getSpecificPost(selectedCategory, selectedPost)),
fetchPosts: (selectedCategory) => dispatch(fetchPosts(selectedCategory))
}
}
export default withRouter(connect(
mapStateToProps,
mapDispatchToProps
)(PostPreview))
我正在使用调用我的 api:
的 redux-thunk 操作调用 export function getSpecificPost(selectedCategory, selectedPost) {
console.log('getSpecificPost', selectedPost)
return dispatch => {
dispatch(beginFetchPosts());
return fetch(selectedPost ? `${api}/${selectedCategory}/${selectedPost}`
: null, { headers })
.then(
res => res.json(),
error => console.log('An error occured at getSpecificPost', error)
)
.then(json => {
dispatch(selectPostSuccess(json));
return json
})
}
}
还有一个减速器:
const initialState = {
posts: [],
categories: [],
loading: false,
error: null,
selectedCategory: 'all',
selectedPost: [],
};
export function postsReducer(state = readableInitialState, action) {
switch(action.type) {
case C.SELECTED_POST:
return {
...state,
loading: false,
selectedPost: action.payload.selectedPost
};
...是的,我用默认语句完成了 switch 语句,据我所知没有语法问题)
每当我看到错误时
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
我回想起第一次设置项目并访问远程服务器进行开发时必须解决的 CORS 问题。
从 chrome 开发工具转到此页面时,您能否提供网络响应? JSON 的开头不应该有 <
,所以我认为您没有反应路由器问题,而是某种服务器配置问题。
问题是你没有提到方法,是GET
,PUT
,所以服务器正在考虑OPTIONS
。您可以以不同的方式使用 fetch
调用。
export function getSpecificPost(selectedCategory, selectedPost){
return (dispatch) => {
const baseURL = selectedPost ? `${api}/${selectedCategory}/${selectedPost}` : null
fetch(baseURL, {
method: 'GET',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(`${headers}`)
})
.then(json => {
dispatch(selectPostSuccess(json))
})
}
}
您还可以通过以下代码片段将数据发送到减速器而不是 json。
export function getSpecificPost(selectedCategory, selectedPost){
return (dispatch) => {
const baseURL = selectedPost ? `${api}/${selectedCategory}/${selectedPost}` : null
fetch(baseURL, {
method: 'GET',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(`${headers}`)
})
.then(json => response.json())
.then(data => {
dispatch(selectPostSuccess(data))
})
}
}
希望对您有所帮助。