Support » Developing with WordPress » ReactJS in WP(wp.elemet)

  • Hello everybody, example from official react site

    
    class Clock extends React.Component {
      constructor(props) {
        super(props);
        this.state = {date: new Date()};
      }
    
      componentDidMount() {
        this.timerID = setInterval(
          () => this.tick(),
          1000
        );
      }
    
      componentWillUnmount() {
        clearInterval(this.timerID);
      }
    
      tick() {
        this.setState({
          date: new Date()
        });
      }
    
      render() {
        return (
          <div>
            <h1>Hello, world!</h1>
            <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
          </div>
        );
      }
    }
    
    ReactDOM.render(
      <Clock />,
      document.getElementById('root')
    );
    

    how this code will be looked when i call wp.element?
    what features of reactjs are saved?
    how get WP REST API link to get JSON data?

    Thx for help

Viewing 1 replies (of 1 total)
  • Hardeep Asrani

    (@hardeepasrani)

    Hey @cheater111,

    wp.element is basically a React wrapper. Your code can look like this if you use wp.element

    const { Component, render } = wp.element;
    
    class Clock extends Component {
    ...rest of the code
    }
    
    render(
      <Clock />,
      document.getElementById('root')
    );

    Or you can keep using React too as it already ships with WordPress, in which case set it as an external in webpack so it isn’t bundled with your build and instead takes it from the WP Core.

    Hope this helps!

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.