简体   繁体   English

如何在 redux state 中保存搜索词?

[英]How do I save search terms in the redux state?

I'm learning redux by using it with a react app that pulls articles from the Hacker News API.我正在学习 redux,它与一个从黑客新闻 API 中提取文章的反应应用程序一起使用。

I want to save the search terms inputted in the search bar to the redux state, but when I look at the state, the searchTerm state I have made is empty in redux devtools. I want to save the search terms inputted in the search bar to the redux state, but when I look at the state, the searchTerm state I have made is empty in redux devtools.

Here is my search bar component:这是我的搜索栏组件:

import React, { Component } from 'react'
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import getSearchTerm from '../actions/searchAction';

class Search extends Component {
constructor(props) {
    super(props);
    this.state = {
        searchTerm: ''
    };

    this.onChange = this.onChange.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
}

onChange(e) {
    this.setState({ [e.target.name]: e.target.value });
}

onSubmit(e) {
    e.preventDefault()
    const searchTerm = this.state.searchTerm

    this.props.getSearchTerm(searchTerm);
}

render() {
    return (
        <div>
            <form onSubmit={this.onSubmit}>
                <input
                    type="text"
                    name="searchTerm"
                    style={{
                        width: "95%",
                        paddingTop: 8,
                        paddingBottom: 8,
                        paddingLeft: 16,
                        fontSize: 24
                    }}
                    placeholder="Enter term here" onChange={this.onChange}
                    value={this.state.searchTerm} />
                <button type="submit">Submit</button>
            </form>
        </div>
    )
}
}


Search.propTypes = {
    searchTerm: PropTypes.string.isRequired
}

const mapPropsToState = state => ({
    getSearchTerm: state.searchTerm
})

export default connect(mapPropsToState, { getSearchTerm })(Search)

Here is the action I am using to save the search term:这是我用来保存搜索词的操作:

export const getSearchTerm = term => dispatch => {
console.log('action called')
dispatch({
    type: GET_TERM,
    term
})
}

And here is the reducer:这是减速器:

import GET_TERM from '../actions/types';

const initState = {
    searchTerm: null
}

export default function (state = initState, action = {}) {
    switch (action.type) {
        case GET_TERM:
            return {
                searchTerm: action.term
            }
        default:
            return state
    }
}

I am also getting a warning in my console saying:我还在控制台中收到警告说:

'Failed prop type: The prop searchTerm is marked as required in Search , but its value is undefined .' '失败的道具类型:道具searchTermSearch中被标记为必填,但其值为undefined

Obviously, this means the search term I have required is undefined because I have it as undefined in the reducer.显然,这意味着我需要的搜索词是未定义的,因为我在 reducer 中将其设置为未定义。 However, I want to save the search term I enter into the redux state.但是,我想保存我在 redux state 中输入的搜索词。 How do I go about this?我该如何 go 关于这个?

Can your check if your reducer is getting it.你能检查一下你的减速器是否得到它。 Just console.log() it before returning new state in your reducer.在你的减速器中返回新的 state 之前,只需 console.log() 即可。

Also use should always spread the state in every return in order to ensure that your overall state doesn't get overwritten.还应始终在每次退货中散布 state,以确保您的整体 state 不会被覆盖。

Posted as I don't have comment privileges.由于我没有评论权限而发布。

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

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