简体   繁体   English

使用create-react-app在React中实现Redux-简单问题

[英]Implementing Redux in React using the create-react-app -Simple question

Is is correct to pass a reducer as props when i'm using a rootreducer ? 当我使用rootreducer时将减速器作为道具传递是否正确?

This is my rootReducer.js : 这是我的rootReducer.js:

import { combineReducers } from 'redux';
import simpleReducer from './simpleReducer';
import messageReducer from './messageReducer';
import NewReducer from './NewReducer';
export default combineReducers({
 simpleReducer,messageReducer,NewReducer

});

And this is one of my action creators addMessage.js 这是我的动作创建者之一addMessage.js

export const addMessage = (message) => dispatch => {
dispatch({
type: 'ADD',
message: message
})
}

Here is the first reducer messageReducer.js 这是第一个reducer messageReducer.js

 export default (state = [], action) => { switch (action.type) { case 'ADD': return [ ...state, action.message ]; default: return state; } }; 

And here is another one simpleReducer.js 这是另一个simpleReducer.js

 export default (state = {}, action) => { switch (action.type) { case 'SIMPLE_ACTION': return { result: action.payload } default: return state } } 
And finally here is my last reducer NewReducer.js 最后是我的最后一个减速器NewReducer.js

 export default (state = '', action) => { switch (action.type) { case 'AnyThing': return action.WhatToDisplay; default: return state; } }; 

Here is my mapping in the App.js 这是我在App.js中的映射

class App extends Component {
    constructor(props) {
    super(props);
    this.state = {
      input: ''

    }
    this.handleChange = this.handleChange.bind(this);
    this.submitMessage = this.submitMessage.bind(this);
    }
    handleChange(event) {
    this.setState({
      input: event.target.value
    });
    }
    submitMessage() {
    this.props.submitNewMessage(this.state.input);
    this.setState({
      input: ''
    });
    }
    simpleAction = (event) => {
    this.props.simpleAction();
    }
    localNormalFunction=(event)=>{
     this.props.NewAction()
    }
    render() {
    return (
      <div >
    <h1>fjasgdasdsg</h1>
    <button onClick={this.simpleAction}>Test redux action</button>
    <pre>
    {
    JSON.stringify(this.props)
    }
         </pre>
         <h2>Type in a new Message:</h2>
        <input
          value={this.state.input}
          onChange={this.handleChange}/><br/> 
          <button onClick={this.submitMessage}>Submit</button>
           <ul>
          {this.props.messageReducer.map( (message,idx) => {
              return (
                 <li key={idx}>{message}</li>
              )
            })
          }
          </ul><br/><br/>
          <button onClick={this.localNormalFunction}>dsadsdsa</button>
          <h2>{this.props.NewReducer}</h2>
          <h2>{this.props.simpleReducer.result}</h2>

          </div>
            );
            }
            }

And here is my ِApp Component.Notice my last 2 h2 tags as well as my ul tag .Without me adding the reducer at the end of the prop , it doesn't work.So is what i'm doing right ? 这是我的ِ App Component.Notice我的最后2个h2标签以及ul标签。没有在道具的末尾添加reducer,那是行不通的。我在做什么对不对? or is there another way to show the redux state in my react ?.Note that i currently have no errors and the code functions well.I just wana know if what i am doing is right or wrong and if there is a better syntax to show the redux state in my create react app. 还是有其他方法可以在我的react中显示redux状态?请注意,我目前没有错误并且代码运行良好。我只是想知道我在做什么是对还是错,以及是否有更好的语法显示我的create react应用中的redux状态。

 class App extends Component { constructor(props) { super(props); this.state = { input: '' } this.handleChange = this.handleChange.bind(this); this.submitMessage = this.submitMessage.bind(this); } handleChange(event) { this.setState({ input: event.target.value }); } submitMessage() { this.props.submitNewMessage(this.state.input); this.setState({ input: '' }); } simpleAction = (event) => { this.props.simpleAction(); } localNormalFunction=(event)=>{ this.props.NewAction() } render() { return ( <div > <h1>fjasgdasdsg</h1> <button onClick={this.simpleAction}>Test redux action</button> <pre> { JSON.stringify(this.props) } </pre> <h2>Type in a new Message:</h2> <input value={this.state.input} onChange={this.handleChange}/><br/> <button onClick={this.submitMessage}>Submit</button> <ul> {this.props.messageReducer.map( (message,idx) => { return ( <li key={idx}>{message}</li> ) }) } </ul><br/><br/> <button onClick={this.localNormalFunction}>dsadsdsa</button> <h2>{this.props.NewReducer}</h2> <h2>{this.props.simpleReducer.result}</h2> </div> ); } } 

It is better practice to get only the props you need from redux in each component. 更好的做法是从每个组件的redux中仅获取所需的道具。 If you pass the whole redux state in mapStateToProps then whenever anything in redux changes you will have everything rerendering even if nothing you use changed. 如果您在mapStateToProps中传递整个redux状态,那么只要redux中的任何内容发生更改,您就将重新呈现所有内容,即使您使用的内容没有任何变化。

One common reason you might be getting errors is that you are trying to use the props in render and they get instantiated afterwards. 可能会出现错误的一个常见原因是,您尝试在渲染中使用这些道具,然后将它们实例化。

Try this give default values to the props if you can't get them from redux: 如果无法从redux获取道具,请尝试以下操作为道具赋予默认值:

App.defaultProps = {
  result: '',
  NewReducer: '',
  messageReducer: []
}

const mapStateToProps = state => ({
  result: state.simpleReducer.result,
  NewReducer: state.NewReducer,
  messageReducer: state.messageReducer
})

and then change this.props.simpleReducer.result to this.props.result 然后将this.props.simpleReducer.result更改为this.props.result

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

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