[英]Store data fetch in a local state redux reactjs
I try to store date of a fetch in local state and not in the global store of my app.我尝试将获取日期存储在本地 state 中,而不是在我的应用程序的全局存储中。
(...)
.then(response => response.json())
.then (data => dispatch(fetchSuccess(data)))
And then:接着:
export const FETCH_SUCCESS = 'FETCH_SUCCESS';
function fetchSuccess(create) {
return {type : FETCH_SUCCESS, payload: create}
}
I have my data in the reducer with this:我在减速器中有我的数据:
const initialState = {
eCreate: ''
};
case FETCH_SUCCESS:
return {...state, eCreate: action.payload}
And after I have my state here in my component:在我的组件中有我的 state 之后:
rEvt : state.redu.eCreate
And I can display with:我可以显示:
console.log(rEvt)
But my state eCreate is available in all my app.但是我的 state eCreate 在我的所有应用程序中都可用。 I just want that this state was available just in this component.
我只是希望这个 state 在这个组件中可用。 How can i do this?
我怎样才能做到这一点? Thanks for help.
感谢帮助。
My component我的组件
function Create(props) {
const {cEvt, sEvt, gtf, rEvt} = props;
console.log(rEvt)
if (rEvt.name === sEvt)
{
return <Redirect to={"/d/" + rEvt.name} />
}
return (
<div >
<input type="text" value={sEvt} onChange={e => cEvt(e.target.value)} />
<button onClick={() => gtf(sEvt)} >Go !</button>
</div>
);
}
And the container of my compo:还有我的组合容器:
const mapStateToProps = (state) => ({
sEvt : state.redu.sCreate,
rEvt : state.redu.eCreate
})
const mapDispatchToProps = dispatch => ({
cEvt : e => dispatch(changeInput(e)),
gtf : (name) => dispatch(fetchCreate(name))
})
My fetch has a method POST to create a new entry with the name of the input.我的 fetch 有一个方法 POST 来创建一个带有输入名称的新条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.