React函数不返回<div>元素

时间:2018-06-01 12:57:56

标签: reactjs

在以下代码中:

class App extends React.Component {
    state = { val: '' };

    handleChange = (e) => {
        this.setState({ val: e.target.value });
    }

    OddEven(num) {
        const number = parseInt(num);
        let description;

        if (Number.isInteger(number)) {
            if (number % 2 == 0) {
                description = <strong>even</strong>;
            } else {
                description = <i>odd</i>;
            }

            alert(number);

            return (
                <h1>
                    Test
                </h1>
            );
        } else {
            return null;
        }
    }
    render() {
        return (
            <div>
                <input type='text' onChange={this.handleChange} />
                <button onClick={() =>this.OddEven(this.state.val) }>
                    Odd Even
                </button>
            </div>
        );
     }
}

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

OddEven()未返回div。无法弄清楚过去2小时的原因。我正处于学习阶段。所以,如果它是愚蠢的话,不要生气。

3 个答案:

答案 0 :(得分:0)

如果您想在此处返回您的说明,请执行以下操作:

auto lol = map_from_il<map_type>({ {"a", iptr{ new int {3} } }, {"b", iptr{ new int {2} } } });

答案 1 :(得分:0)

那是因为你没有渲染你的函数在任何地方返回的东西,你宁愿做的是:

public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button generateQbutton;
private boolean aBoolean;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textV);
    generateQbutton = (Button) findViewById(R.id.buttonP);

    randomFacts ();

    generateQbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder altdial = new AlertDialog.Builder(MainActivity.this);
            altdial.setMessage(factArray[0].getmDialog()).setPositiveButton("k", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            AlertDialog ctnalert =  altdial.create();
            ctnalert.show();

            randomFacts ();

        }
    });

}
public void randomFacts (){
    shuffelFacts ();
    textView.setText(factArray[0].getmFacts());
    aBoolean = factArray[0].ismAnswers();

}
Facts f01 = new Facts(true, "Quesstion1?", "Dialog for Q1!");
Facts f02 = new Facts(false, "Quesstion2??", "Dialog for Q2!");
Facts f03 = new Facts(true, "Quesstion3??", "Dialog for Q3!");
Facts f04 = new Facts(false, "Quesstion4??", "Dialog for Q4!");
Facts f05 = new Facts(true, "Quesstion5?", "Dialog for Q5!");


Facts [] factArray = new Facts[]{
        f01,f02,f03,f04,f05,
};
public void shuffelFacts(){
    Collections.shuffle(Arrays.asList(factArray));
}
}

这里我们将状态设置为您想要渲染的内容,然后在渲染函数中使用它。

答案 2 :(得分:0)

试试这个:

class App extends React.Component {
state = { val: '' };

handleChange = (e) => {
    this.setState({ val: e.target.value });
}

OddEven(num) {
    const number = parseInt(num);
    if (Number.isInteger(number)) {
        if (number % 2 == 0) {
            return <strong>even</strong>;
        } else {
            return <i>odd</i>;
        }

    } else {
        return <a>Please Enter A Number</a>;
    }
}
render() {
    return (
        <div>
            <input type='text' onChange={this.handleChange} />
            {OddEven(this.state.val)}
        </div>
        );
}