1

I want to fetch the image from the URL and to display it.

I take the url as input in URL text field and when I use that in img tag it works like but when I pass or use it in td background it doesn't work

import React, { Component } from 'react';


class ImageStyle extends Component {

  constructor(props) {
        super(props);

        this.state = {
            title: '',
            url: '',
            summary: ''
        };

        this.handleInputChange = this.handleInputChange.bind(this);
    }

    handleInputChange(event) {

        this.setState({
            [event.target.name]: event.target.value
        });

    }

  render() {

    return ( 
      <div>
        <div>
          <h1 className="row px-2">Image Style Notification</h1>
          <hr />
          <div className="row px-1 py-2 animated fadeIn">

                <div className="px-1" style={{ width:50 + '%' }}><br />

                    <div className="mb-1">
                      <input type="text"
                       className="form-control" 
                       placeholder="Title"
                       name="title"
                       value={this.state.title}
                       onChange={this.handleInputChange}
                       />
                    </div>

                    <div className="mb-1">
                      <textarea 
                      className="form-control" 
                      placeholder="Image URL"
                      name="url"
                      value={this.state.url}
                       onChange={this.handleInputChange}
                      />
                    </div>

                    <div className="mb-1">
                      <textarea
                       className="form-control" 
                       placeholder="Summary"
                       name="summary"
                       value={this.state.summary}
                       onChange={this.handleInputChange}
                       />
                    </div>

                    <br />

                    <div className="row px-2" >
                      <div>
                        <button className="nav-link btn btn-block btn-info"  onClick={this.handleClick} >Save</button>
                      </div>
                      <div className="px-1">
                        <button className="nav-link btn btn-block btn-danger"> Cancel</button>
                      </div>
                    </div><br />

                </div>
                <div><br />
                  <div className="mobile">
                      <table className="table table-clear width-css">
                      <tbody>
                        <tr>
                          <td backgroundImage: 'url(${this.state.url})'>
                            <strong>
                              {this.state.title}
                            </strong><br />
                           {this.state.summary}<br /> 
                          </td>
                        </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
          </div>
    </div>
    </div>
    )

  }
}

export default ImageStyle;

1 Answer 1

1

Specify the styles for the table and mention background image url like

 backgroundImage: `url(${this.state.url})`

class ImageStyle extends Component {

  constructor(props) {
        super(props);

        this.state = {
            title: '',
            url: '',
            summary: ''
        };

        this.handleInputChange = this.handleInputChange.bind(this);
    }

    handleInputChange(event) {

        this.setState({
            [event.target.name]: event.target.value
        });

    }

  render() {
    var styles = {
      backgroundImage: `url(${this.state.url})`
    }
    return ( 
      <div>
        <div>
          <h1 className="row px-2">Image Style Notification</h1>
          <hr />
          <div className="row px-1 py-2 animated fadeIn">

                <div className="px-1" style={{ width:50 + '%' }}><br />

                    <div className="mb-1">
                      <input type="text"
                       className="form-control" 
                       placeholder="Title"
                       name="title"
                       value={this.state.title}
                       onChange={this.handleInputChange}
                       />
                    </div>

                    <div className="mb-1">
                      <textarea 
                      className="form-control" 
                      placeholder="Image URL"
                      name="url"
                      value={this.state.url}
                       onChange={this.handleInputChange}
                      />
                    </div>

                    <div className="mb-1">
                      <textarea
                       className="form-control" 
                       placeholder="Summary"
                       name="summary"
                       value={this.state.summary}
                       onChange={this.handleInputChange}
                       />
                    </div>

                    <br />

                    <div className="row px-2" >
                      <div>
                        <button className="nav-link btn btn-block btn-info"  onClick={this.handleClick} >Save</button>
                      </div>
                      <div className="px-1">
                        <button className="nav-link btn btn-block btn-danger"> Cancel</button>
                      </div>
                    </div><br />

                </div>
                <div><br />
                  <div className="mobile">
                      <table className="table table-clear width-css">
                      <tbody>
                        <tr>
                          <td style={styles}>
                            <strong>
                              {this.state.title}
                            </strong><br />
                           {this.state.summary}<br /> 
                          </td>
                        </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
          </div>
    </div>
    </div>
    )

  }
}

Sign up to request clarification or add additional context in comments.

6 Comments

i tried a bit different approach Can you help in this? stackoverflow.com/questions/43408824/…
if you could help in link provided it will be a huge boost as it will be used at several other places stackoverflow.com/questions/43408824/…
Piyush, can you try this and let me know if it works
Do u know how to multi select dropdown elements? ill upload then the snippet if you have time to tell
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.