简体   繁体   English

mapStateToProps不更新

[英]mapStateToProps not updating

I am just beginning to play with react/redux. 我刚开始玩react / redux。 I just want to input some text and hit submit and then pass that to another component that will display whatever was input. 我只想输入一些文本并点击提交,然后将其传递给另一个显示输入内容的组件。

I know I can get the data from point A to B because if I use store.subscribe than I can access the state and it is always accurate. 我知道我可以从A点到B点获取数据,因为如果我使用store.subscribe而不是我可以访问状态并且它总是准确的。 I am trying to use mapStateToProps though and I am not having any luck. 我试图使用mapStateToProps,但我没有运气。

I am not using mapDispatchToProps so maybe that is an issue? 我没有使用mapDispatchToProps所以这可能是一个问题? I cant seem to find a good simple example. 我似乎无法找到一个很好的简单例子。 mapStateToProps also only seems to run when I refresh the page (using webpack-dev-server) since it only logs one time on page load and never again. mapStateToProps似乎也只在我刷新页面时运行(使用webpack-dev-server),因为它只在页面加载时记录一次,而不再记录。

 _______________ Input.js _________________ import React from 'react'; import store from '../redux/store'; import { connect } from 'react-redux'; import { addSearchParam } from '../redux/actions'; export default class Input extends React.Component { constructor(props) { super(props); this.state = { player: '' }; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(event) { this.setState({ player: event.target.value }); } handleSubmit(event) { event.preventDefault(); store.dispatch(addSearchParam(this.state.player)) } render() { return ( <form onSubmit = {this.handleSubmit} > <label> <input type="text" value={this.state.player} onChange={this.handleChange}/> </label> <input type="submit" value="Submit" /> </form> ); } } _______________ Info.js _________________ import React from 'react'; import store from '../redux/store'; import { connect } from 'react-redux'; class Info extends React.Component { constructor(props) { super(props); } render() { return ( <h2> {this.props.player}</h2> ) } } function mapStateToProps(state) { console.log("mapStateToPropsInfo: ", state) return { player: state.player } } export default connect(mapStateToProps, null)(Info); _______________ reducers.js _________________ 'use strict'; import { combineReducers } from 'redux'; const SEARCH_PARAM = 'SEARCH_PARAM'; const searchReducer = (state = '', action) => { if (action.type === SEARCH_PARAM) { return action.text; } return state; } export const reducers = combineReducers({ searchReducer }) export default reducers; _______________ actions.js _________________ 'use-strict'; export const addSearchParam = input => { return { type: 'SEARCH_PARAM', id: 'player', text: input } } _______________ index.js _________________ 'use-strict'; import React from 'react'; import ReactDOM from 'react-dom'; import App from './js/App'; import { Provider } from 'react-redux'; import { createStore } from 'redux'; import reducers from './js/redux/reducers' let store = createStore(reducers) ReactDOM.render( <Provider store={store}> <App /> </Provider>, document.getElementById('root') ); 

Those seem to be the most important files related to this problem but I can provide more if necessary. 这些似乎是与此问题相关的最重要的文件,但如果有必要,我可以提供更多。 Thanks for any help. 谢谢你的帮助。 Hopefully im just missing something simple. 希望我只是缺少一些简单的东西。

I think the issue is that you have written actions but never used/connected it. 我认为问题在于你有书面actions但从未使用/连接它。 You need to use mapDispatchToProps in Input.js . 需要Input.js使用mapDispatchToProps

First import the action in input.js like this: 首先在input.js导入action ,如下所示:

import { addSearchParam } from './actions';

Write mapDispatchToProps function like this: 像这样写mapDispatchToProps函数:

function mapDispatchToProps(dispatch){
    return bindActionCreators({addSearchParam}, dispatch);
}

Then, inside Input.js in handleSubmit function do this: 然后,在handleSubmit函数中的Input.js内执行此操作:

this.props.addSearchParam(this.state.player)                

Also, instead of exporting class while declearing, change export statement of Input.js to also bind the mapDispatchToProps : 此外,不是在删除时导出类,而是更改Input.js export语句以绑定mapDispatchToProps

export default connect(null, mapDispatchToProps)(Input);       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM