单击按钮时渲染额外的React组件

时间:2020-08-04 14:09:25

标签: javascript reactjs button components rendering

我正在一个项目上,当我单击按钮时,我尝试使用React渲染一些组件。我是React和JavaScript的新手,所以有点令人困惑。单击按钮时,我设置了组件的状态,但不重新渲染。我的代码:

import React, {PureComponent} from "react";
import {render} from "react-dom";

let websocket = new WebSocket("ws://localhost:8081");
let scenarios = [];

    
websocket.onopen = function(){
    console.log("Frontend client connected");
    websocket.send("Get all scenarios available");
}

websocket.onmessage = function(event){
    const receivedMessage = event.data;
    
    if(receivedMessage.includes("Available scenarios")){
        scenarios = receivedMessage.split("/");
        console.log(scenarios);
    }
}

class App extends PureComponent{
    state = {
        scenariosReceived: false
    };

    handeStartClick = () => {
        this.setState({
            scenariosReceived : true
        })
    };

  
    render()
    {
    
        return(
            <div id="container">
                <button onClick={this.handeStartClick.bind(this)}>Start</button>
                {this.scenariosReceived? (
                <ol className="scenarios_list">
                    <li><button onClick={this.handleScenarioSelection}>{scenarios[1]}</button></li>
                    <li><button onClick={this.handleScenarioSelection}>{scenarios[2]}</button></li>
                    <li><button onClick={this.handleScenarioSelection}>{scenarios[3]}</button></li>
                    <li><button onClick={this.handleScenarioSelection}>{scenarios[4]}</button></li>
                    <li><button onClick={this.handleScenarioSelection}>{scenarios[5]}</button></li>
                    <li><button onClick={this.handleScenarioSelection}>{scenarios[6]}</button></li>
                    <li><button onClick={this.handleScenarioSelection}>{scenarios[7]}</button></li>
                </ol>
                ) :(console.log("No scenarios received"))
                }
            </div>
        );
    }
}

任何想法都会有所帮助。非常感谢!

2 个答案:

答案 0 :(得分:0)

{this.scenariosReceived? (

您在这里没有从状态中获取价值,更正:

{this.state.scenariosReceived? (

答案 1 :(得分:0)

您可以拥有一个状态,该状态包含一个标识该React组件的对象数组,并且单击即可向该数组添加另一个对象。

我将在下面提供我已实现的代码,以在react-native中单击按钮时呈现组件

const [mediaRows, setMediaRow] = useState([
    // here I have a default compont so I have one object by default
    {id: 1, imgUri: '', moreInfo: ''},
]);
// this is the function that executed when the button clicked
const addMediaRow = () => {
  const lastId = mediaRows.length;
  setMediaRow([...mediaRows, {id: lastId + 1, imgUri: ''}]);
};

当该数组更改更改时,将渲染下面的JSX

{mediaRows.map((media) => {
      return (
        <View
          key={media.id}
          style={[
            styles.mediaMediaContainer,
            {backgroundColor: colors.primary},
          ]}>
          <Avatar
            onPress={() => uploadImage(media.id)}
            source={
              media.imgUri
                ? {
                    uri: media.imgUri,
                  }
                : undefined
            }
            rounded
            size="medium"
            containerStyle={{
              backgroundColor: colors.backgroundDegree,
              marginRight: 10,
            }}
            icon={{
              name: 'photo-camera',
              size: 24,
              color: Colors.white,
            }}
          />
          <Input
            placeholder="Type More Information"
            overrideStyles={[
              styles.inputOverrideStyles,
              {backgroundColor: colors.mediaInput},
            ]}
          />
        </View>
      );

react和react-native之间的逻辑-您只需要了解这个想法并更改JSX,快乐的编码兄弟