简体   繁体   English

当我执行 onSubmit 时,我无法显示 state

[英]I am not able to display the state when I do onSubmit

I have the component that it is rendered 4 times in parent component.我有它在父组件中呈现 4 次的组件。 I would like to change the state in parent component and show the array with all the values of the four inputs.我想更改父组件中的 state 并显示包含四个输入的所有值的数组。 Anyway in the parent component when I do onSubmit the state does not seem to be updated and the value is not displayed.无论如何,当我执行 onSubmit 时,在父组件中 state 似乎没有更新,并且没有显示该值。 I think I am passing some props in a wrong way.我想我以错误的方式传递了一些道具。 Could anyone explain me properly what I am doing wrong?谁能正确解释我做错了什么? thanks a lot多谢

import React, { useState } from 'react';
import CurrencyInput from 'react-currency-input-field';

function Stake(props) {
    const [newStake, setStake] = useState(['']);

    const changeStake = (e) => {
        setStake(e.target.value)
    }
    return (
        <>
            <CurrencyInput
                onChange={changeStake}
                newStakeProp={newStake}
                style={{
                    marginLeft: "40px",
                    width: "50px"
                }}
                placeholder="Stake"
                decimalScale={2}
                prefix="£"
            />
            <button onSubmitProp={props.onSubmit}>yes</button>
            <button>no</button>
            {newStake}
        </>
    );
}

export default Stake;
import React, { Component } from 'react';
import Stake from './stake';

class FetchRandomBet extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: true,
            bet: null,
            value: this.props.value,
            allStakes: ['']
        };
    }

    async componentDidMount() {
        const url = "http://localhost:4000/";
        const response = await fetch(url);
        const data = await response.json();

        this.setState({
            loading: false,
            bet: data.bets,
        });
    }

    render() {
        const { valueProp: value } = this.props;
        const { newStakeProp: newStake } = this.props;
        const { bet, loading } = this.state;

        if (loading) {
            return <div>loading..</div>;
        }
        if (!bet) {
            return <div>did not get data</div>;
        }
        return (
            < div >
                {
                    loading || !bet ? (
                        <div>loading..</div>
                    ) : value === 0 ? (
                        <div className="bet-list">
                            <ol>
                                <p>NAME</p>
                                {
                                    bet.map(post => (
                                        <li key={post.id}>
                                            {post.name}
                                        </li>
                                    ))
                                }
                            </ol>
                            <ul>
                                <p>ODDS</p>
                                {
                                    bet.map(post => (
                                        <li key={post.id}>
                                            {post.odds[4].oddsDecimal}
                                            <div className="stake-margin">
                                                <Stake
                                                    allStakes={this.state.allStakes}
                                                    onSubmit={() => { this.setState({ allStakes: [...newStake] }) }}
                                                >
{this.state.allStakes}
                                                </Stake>
                                            </div>
                                        </li>
                                    ))
                                }
                            </ul>
                        </div>
                  

newStake is the state of your Stake component. newStake是您的 Stake 组件的 state。 But the parent cannot know about it if you are not passing it up from the child component.但是如果您没有从子组件传递它,则父级无法知道它。

So the Stake child component should have its own submit function, which then calls the onSubmit of the parent with a parameter holding newStake所以Stake子组件应该有自己的提交function,然后调用父组件的onSubmit ,参数持有newStake

The inline onSubmit in the parent should then accept the parameter holding newStake, of course父级中的内联 onSubmit 然后当然应该接受持有 newStake 的参数

import React, { useState } from 'react';
import CurrencyInput from 'react-currency-input-field';

function Stake(props) {
    const [newStake, setStake] = useState(['']);

    const changeStake = (e) => {
        setStake(e.target.value)
    }

    const mySubmit = () => {
        props.onSubmit(newStake);
    }
    return (
        <>
            <CurrencyInput
                onChange={changeStake}
                style={{
                    marginLeft: "40px",
                    width: "50px"
                }}
                placeholder="Stake"
                decimalScale={2}
                prefix="£"
            />
            <button onSubmitProp={mySubmit}>yes</button>
            <button>no</button>
            {newStake}
        </>
    );
}

export default Stake;

I tried to change onSubmit with onClick and it suddenly worked properly.我尝试使用 onClick 更改 onSubmit 并且它突然正常工作。 Thanks for the help guys谢谢你们的帮助

<Stake
onClick={(newStake) => { this.setState({ allStakes: [...newStake, newStake] }) }}
 />
<button onClick={mySubmit}>yes</button>

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

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