【问题标题】:How to edit a React component and change the text displayed on screen如何编辑 React 组件并更改屏幕上显示的文本
【发布时间】:2018-04-17 22:19:00
【问题描述】:

我有一个添加/删除/编辑组件和一个电影组件。我想在Add里面编辑一个电影组件,然后我想在旧数据显示的地方显示新数据。

这是我的添加/编辑/删除组件 导入 React,{Component} from 'react';

import Movie from './Movie.jsx';


class AddComponent extends Component {
    constructor(props){
        super(props);
        this.state = {
            movieText: '',
            movies: [],
        };
    }

updateMovieText(movieText){
    this.setState({movieText: movieText.target.value})
}

addMovie(){
    if(this.state.movieText === ''){return}

    let moviesArr = this.state.movies;
    moviesArr.push(this.state.movieText);
    this.setState({movieText: ''})
    this.textInput.focus();
}

handleKeyPress = (event) => {//enables to add when pressing enter on keyboard
    if(event.key === 'Enter'){
    let moviesArr = this.state.movies;
    moviesArr.push(this.state.movieText);
    this.setState({movieText: ''})
    }
}

deleteMovie(index) {
    const movies = this.state.movies;
    const newMovies = [
        ...movies.slice(0, index),
        ...movies.slice(index + 1)
    ];
    this.setState({
        movies: newMovies
    });
    // Notice movies !== new Movies
    // and movies still contains all the previous values
}


editMovie(index,value){
    const movies = this.state.movies;
    const newMovies = movies.map((movie, i) => {//HERE is EDIT
        if (i !== index) {
            return movie;
        }
        return value;
    });
    this.setState({ movies: newMovies });
    // Notice movies !== new Movies
    // and movies still contains the previous values
}

    render(){
        let movie = this.state.movies.map((val,key)=> {//prints on screen list of movies see line55
            return (<Movie 
            key={key} 
            text={val} 
            deleteMethod={() => this.deleteMovie(key)}
            editMethod={this.editMovie.bind(this, key)}
             />

            );

        });

        return (
            <div>
                <input type="text"
                    ref={((input)=>{this.textInput = input;})}
                    className="textInput"
                    value={this.state.movieText}
                    onChange={movieText => this.updateMovieText(movieText)}
                    onKeyPress={this.handleKeyPress.bind(this)}
                    />
                <button onClick={this.addMovie.bind(this)}>Add</button>
                {movie}

                </div>

        );
    }
}

export default AddComponent;

这是我的电影组件

import React, {Component} from 'react';


class Movie extends Component{
    constructor(props){
        super(props);
        this.state= {
            inputValue: ''
        };
    }
    updateInputValue(evt) {
        this.setState({
            inputValue: evt.target.value
        });
    }

    render(){
        return(
            <div>
                {this.props.text}
                <button onClick={this.props.deleteMethod}>X</button>
                <input value={this.props.newMovieName} 
                    onChange={evt => this.updateInputValue(evt)}
                /> 
                <button onClick={() => this.props.editMethod(this.inputValue)}>edit</button>


            </div>
        );
    }
}

export default Movie;

使用上面的代码 this.props.text 消失,只留下删除按钮、编辑文本输入和编辑按钮。如果我 console.log 从编辑函数返回的值打印'undefined'

【问题讨论】:

    标签: javascript node.js reactjs web


    【解决方案1】:

    我猜你想使用this.state.inputValue 而不是this.inputValue

    【讨论】:

      【解决方案2】:

      您只需将状态传递给编辑方法:

      <button onClick={() => 
      this.props.editMethod(this.state.inputValue)}>edit</button>
      

      【讨论】:

        猜你喜欢
        • 2010-10-02
        • 2017-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-07
        相关资源
        最近更新 更多