简体   繁体   English

对象不是有效的React子级Redux

[英]Objects are not valid as a React child, Redux

I have a React Movie class that let you input a movieName . 我有一个React Movie类,可让您输入movieName You can also view all the movies you entered. 您还可以查看所有输入的电影。

I am now updating my app with Redux but I am having a problem when I want to display the list. 我现在正在使用Redux更新我的应用程序,但是当我想显示列表时遇到问题。

Here is what is already working in React: 这是已经在React中工作的东西:

import React from 'react';
import { render } from 'react-dom';

class App extends React.Component {
     state = {
      favorite: false,
      movieName: "",
      movies: [],
      filter: true,
    };

  handleChange = (event) => {
    event.target.name === "favorite"
      ? this.setState({[event.target.name]: event.target.checked})
      : this.setState({[event.target.name]: event.target.value});
  }

  handleSubmit = (event) => {
    event.preventDefault();
    this.addMovie();
  }

  addMovie = () =>{
    this.setState({
      movies: [...this.state.movies, {name: this.state.movieName, favorite: this.state.favorite }]
    });
  }

  listFavoriteMovies = () => (
      <ul>
        {this.state.movies
        .filter( movie => movie.favorite )
        .map( movie => <li>{movie.name}</li>)}
      </ul>
    );

    listAllMovies = () => (
      <ul>
        {this.state.movies
          .map(movie => <li>{movie.name}</li>)}
      </ul>
    );

  changeFilter = () =>
    this.setState( prevState => ( {
      filter: !prevState.filter,
    }))


  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <label>
            Movie name:
            <input name="movieName" type="text" onChange={this.handleChange} />
            Favorite?
            <input name="favorite" type="checkbox" onChange={this.handleChange} />
          </label>
          <input type="submit" value="Submit" />
        </form>
        <ul>
          {
            this.state.filter
            ? this.listFavoriteMovies()
            : this.listAllMovies()
            }
        </ul>
        <button onClick={this.changeFilter}>Click toggle for all/favorites.</button>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

This is the update I am trying to implement and is not working: 这是我尝试实施的更新,但无法正常工作:

listAllMovies = () => (//
  <ul>
      {this.props.dispatch({ type: 'LIST_ALL_MOVIES' })}
  </ul>
);

I get this error: 我收到此错误:

Objects are not valid as a React child (found: object with keys {type}). 对象作为React子对象无效(找到:带有键{type}的对象)。 If you meant to render a collection of children, use an array instead. 如果要渲染子级集合,请改用数组。

I don't know how should I return the state in the reducer function. 我不知道如何在化reducer函数中返回状态。 Should I return the <ul> in reducer ? 我应该在reducer返回<ul>吗?

Another question I have: should I apply Redux in functions like 我还有另一个问题:我应该在类似的函数中应用Redux吗?

  handleChange = (event) => {//?
    event.target.name === "favorite"
      ? this.setState({[event.target.name]: event.target.checked})
      : this.setState({[event.target.name]: event.target.value});
  }

and

  changeFilter = () => //
    this.setState( prevState => ( {
      filter: !prevState.filter,
    }))

? How? 怎么样?

since you are using redux, you should connect your react component with Redux so that you can access Redux State, ie the app level movie state in redux. 由于您使用的是Redux,因此应将react组件与Redux连接,以便可以访问Redux State,即Redux中的应用程序级别电影状态。 Your top level js (app.js) should also implement store provider to save the app state. 您的顶级js(app.js)还应该实现商店提供程序以保存应用程序状态。

You should try to map the app state to props (mapStateToProps), and set the element to map the state from props. 您应该尝试将应用程序状态映射到props(mapStateToProps),并设置元素以从props映射状态。

some good reference you can find it here 一些很好的参考,你可以在这里找到

Hope it helps 希望能帮助到你

  1. You cannot dispatch to JSX. 您不能分派到JSX。 Dispatch is used to send information to redux, where it is captured by reducers and middleware and that returns a data which will be stored in redux. Dispatch用于将信息发送到redux,reduce和中间件在其中捕获信息,并返回将存储在redux中的数据。 So dispatching something in JSX(inside the return of your render) will cause error. 因此,在JSX中分发某些内容(在渲染的返回内部)将导致错误。

  2. I don't know how should I return the state in the reducer function Reducer has nothing to do with your JSX. 我不知道我应该如何在reducer函数中返回状态 Reducer与您的JSX无关。 Reducer returns immutable state values which will be stored in store. Reducer返回不可变状态值,该状态值将存储在商店中。 You an only access these data from the store using this.props.state_variable_name after connecting your component to store using connect() . 在使用connect()将组件连接到存储之后,您只能使用this.props.state_variable_name从存储访问这些数据。

  3. should I apply Redux in functions Redux can be considered as a temporary storage area for your application, where you have listeners(your components) who listen to changes in these data objects 我应该在功能中应用Redux吗?Redux可以被认为是应用程序的临时存储区,在那里您有监听器(您的组件)来监听这些数据对象的变化。

I would say you should listen to this video to have a brief understanding of redux easily, it a very simple concept. 我要说的是,您应该听此视频以轻松了解redux,这是一个非常简单的概念。

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

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