【问题标题】:React redux inputs反应 redux 输入
【发布时间】:2020-10-27 10:38:45
【问题描述】:

这是一个反应形式

const mapStateToProps = state => {
    return {
        number:state.number,
        id: state.id
    }
   
}
const mapDispatchToProps = (dispatch) => {
    return {
        submitInput: (event) => {
        dispatch(handleChanger(event))
      }
    }
  };
class  Id extends React.Component {
    constructor(){
        super ();

        this.state = {
            NUMBER:'',
            ID:''
        }
        this.handleChange = this.handleChange.bind(this);
    }

    handleChange(events) {
        this.setState({
            [events.target.name]:events.target.value
        });
      }

    render(){
        return (
            <div className="bodywrapper">
                <div className="projectcontainer">
                    <div className="textcontainer">
                        <div className="textheader">Files required</div>
                    </div>
                    <div className="bodycontainer">
                        <div className="fileinput"> 
                            <input className="idforminput" 
                                placeholder="Project name" 
                                type="text"
                                onChange = {this.handleChange}
                                value = {this.state.ID}
                                name = "ID"
                                /></div>    
                        <div className="idtextcontainer">
                            <div className="filetext fileinput">Number of slides required</div>
                        <div className="fileinput"> 
                            <input className="idformnumberinput" 
                                   placeholder="10" 
                                   type="number" 
                                   onChange = {this.handleChange}
                                   onChange = {this.props.submitInput(this.state.ID)}
                                   name = "NUMBER" 
                                   value={this.state.NUMBER}/> 
                            </div>     
                        </div>      
                        {this.props.ID}    
                    </div>
                    <div className="buttoncontainer">
                        <button className="bottontext" 
                        
                                onClick ={this.props.submitInput(this.state.ID) }>
                                <a href="link.html">Next step</a> 
                        </button>
                    </div>
                </div>
             </div>
        )
    }

    
}
export default connect(mapStateToProps,mapDispatchToProps)(Id)

handlechage 将输入值添加到状态。

我需要全局的状态,所以使用了 redux。 MDTP 使用 handleChanger 动作将值添加到 reducer:

export const handleChanger = (event) => ({
        type:event.target.name,
        event:event
    })

所以类型变成了ID它会在reducer的状态中添加相应的值:

export const handleChanger = (state = initialState,action) => {
    switch (action.type) {
        case NUMBER:
            return{
                ...state,
                number: action.event
            }
        case ID:
            return {
                ...state,
                id: action.event
            }
        default: return state
    }
}

那么为什么这段代码不起作用? 或者你能建议任何其他方法来向 redux 状态添加输入吗?

【问题讨论】:

    标签: reactjs input redux


    【解决方案1】:

    您不必像在 redux 存储中那样维护组件中的状态。 请检查以下代码

    // reducer 
    const initialState = { 
        ID: 0,
        Number: ''
    }
    const inputDataReducer = (state=initialState,action) => {
        switch(action.type) {
            case 'SET_INPUT_DATA': {
                return {
                    ...state,
                    ...action.payload
                }
            }
            default: return state;
        }
    }
    
    
    // action 
    export function setInputDataToRedux(name,value) {
        return {
            type:'SET_INPUT_DATA',
            payload: {
                [name]:value
            }
    
        }
    }
    
    
    class Id extends React.Component {
        constructor(props){
            super(props);
            this.handleChange = this.handleChange.bind(this);
        }
    
        handleChange(events) {
            this.props.setInputDataToRedux(events.target.name,events.target.value);
          }
    
        render(){
            return (
                <div className="bodywrapper">
                    <div className="projectcontainer">
                        <div className="textcontainer">
                            <div className="textheader">Files required</div>
                        </div>
                        <div className="bodycontainer">
                            <div className="fileinput"> 
                                <input className="idforminput" 
                                    placeholder="Project name" 
                                    type="text"
                                    onChange = {this.handleChange}
                                    value = {this.props.ID}
                                    name = "ID"
                                    /></div>    
                            <div className="idtextcontainer">
                                <div className="filetext fileinput">Number of slides required</div>
                            <div className="fileinput"> 
                                    <input className="idformnumberinput" 
                                       placeholder="10" 
                                       type="number" 
                                       onChange = {this.handleChange}
                                       onChange = {this.props.submitInput(this.state.ID)}
                                       name = "NUMBER" 
                                       value={this.props.NUMBER} 
                                    /> 
                                </div>     
                            </div>      
                            {this.props.ID}    
                        </div>
                        <div className="buttoncontainer">
                            <button className="bottontext" 
                            
                                    onClick ={this.props.submitInput(this.state.ID) }>
                                    <a href="link.html">Next step</a> 
                            </button>
                        </div>
                    </div>
                 </div>
            )
        }
    
        
    }
    const mapStateToProps = (state) => ({
        // fetch both id and number from reducer
        ID: state.reducer.ID, // add your reducer
        Number: state.reducer.number // add your reducer
    })
    export default connect(mapStateToProps,mapDispatchToProps)(Id)
    

    【讨论】:

    • 为什么没有 mapDispatchToProps ?
    • 您可以使用您创建的相同 mapDispatchToProps,但请确保添加 setInputDataToRedux
    • 我们设置了导出函数 setInputDataToRedux(name,value) ,其中 name 是 input name="ID" 的属性,value 为 value ={this.props.ID} ,所以我们可以省略const initialState = { ID: 0, Number: '' } 对吗?
    • initialState 在 reducer 中。因此,如果您省略了 initialState,它会将存储中的 id 和 name 作为 undefined 传递,并且输入将引发不受控制的组件警告。或者你可以使用 defaultProps 来传递 id 和 number 的 initialValue 使用 prop-types npm package ,如果你使用的是一个......组件需要有一个初始值,就像你在组件状态中维护的那个一样。跨度>
    猜你喜欢
    • 2018-05-18
    • 2020-10-05
    • 2018-03-20
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2022-08-16
    • 1970-01-01
    相关资源
    最近更新 更多