Javascript Can';t在redux操作中重命名响应
我想重命名Javascript Can';t在redux操作中重命名响应,javascript,reactjs,redux,action,Javascript,Reactjs,Redux,Action,我想重命名中的响应。然后(…),因为它的调用与参数相同,但是 [1] ./src/actions/postsActions.js [1] Line 95: 'e' is not defined no-undef 问题是它调度显示消息的操作,并将addPost参数数据作为showMessage参数,并且其中没有消息属性 例如: export const addPost = data => dispatch => { dispatch({ type: ADD_POS
中的响应。然后(…
),因为它的调用与参数相同,但是
[1] ./src/actions/postsActions.js
[1] Line 95: 'e' is not defined no-undef
问题是它调度显示消息的操作
,并将addPost
参数数据作为showMessage
参数,并且其中没有消息属性
例如:
export const addPost = data => dispatch => {
dispatch({
type: ADD_POST
});
fetch(API_URL + "/posts", {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
Authorization: JSON.parse(localStorage.getItem("token")),
"Access-Control-Allow-Origin": "*"
}
})
.then(res => res.json())
.then(
e =>
dispatch({
type: ADD_POST_SUCCESS,
payload: e
}),
dispatch(showMessage(e)),
setTimeout(() => {
history.push("/");
dispatch({ type: CLEAR_MESSAGE });
}, 2000)
)
.catch(err =>
dispatch({
type: ADD_POST_FAILED,
payload: err
})
);
};
而不是
ecma6
implicitreturn
,添加一个block语句
不需要在那里返回。用分号分隔语句
then(e => {
dispatch({type: ADD_POST_SUCCESS, payload: e});
dispatch(showMessage(e));
setTimeout(() => {
history.push("/");
dispatch({type: CLEAR_MESSAGE});
}, 2000);
});
请考虑下面的代码,当您将多个函数链接到<代码> > <代码>时,引用中断
Promise.resolve(1)
.then(
e =>
console.log(e),
console.log(e) // this will give an error, as e is not accessible here
)
要么你必须像其他人建议的那样把所有东西都封装在一个块中,要么如果执行顺序很重要,你可以这样做
Promise.resolve(1)
.then(e => {
console.log('1. ' + e); // first function
return e;
})
.then(e => {
console.log('2. ' + e); // second function
return e;
})