1

I'm going to build a web with react js. I'm very new to react so I have a problem to make CRUD. For the first I want to display some data json to table. I have file .js that named keranjang.js. it contains jsx to display the table. and I have another named barang.js and want to fill it with the method named tampildata() used to hold json data for example {"nama_barang" : "bolu", "harga" : "10000"}. How I write the method? and how do I call the method and data to display that data into the existing table in keranjang.js? Hope everyone helps me.

3
  • Can you post some code showing what you've already tried? Commented Dec 7, 2017 at 15:18
  • You are trying to access method that is present in one js file returning json and want to display that json in another js file that having table ui. Correct? Commented Dec 7, 2017 at 15:25
  • yes you're right bro Commented Dec 8, 2017 at 14:57

1 Answer 1

4

I am assuming that you trying to call external file's method in current component. In your barang.js file export your function that holds json data like

export function tampildata() {
    return [{ "firstname": "first", "lastname": "f"}, {"firstname": "second", "lastname": "l"}];
}

or

export default {
  tampildata() {
    return [{ "firstname": "first", "lastname": "f"}, {"firstname": "second", "lastname": "l"}];
  }
};

Then in your keranjang.js file import your tampildata method and call that method in componentDidMount and set state like below

import React from 'react';
import ReactDOM from 'react-dom';
import { tampildata } from 'barang';

class TableComponent extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            json: []
        }
    }

    componentDidMount() {
        this.setState((prevState) => {
            return {
                json: tampildata()
            }
        })
    }

    render() {
        return (
            <div>
                <table>
                    <thead>
                        <th>First Name</th>
                        <th>Last Name</th>
                    </thead>
                    <tbody>
                        {this.state.json.map((data, i) => {
                            return (
                                <tr key={i}>
                                    <td>{data.firstname}</td>
                                    <td>{data.lastname}</td>
                                </tr>
                            )
                        })}
                    </tbody>
                </table>
            </div>
        )
    }
}


ReactDOM.render(
  <TableComponent />, 
  document.getElementById("app")
);

Here is the working jsfiddle. Hope this will helps you!

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

5 Comments

hey, but i have problem with exporting the method from first .js file. how to export the method? is this ..export function tampildata() {}.. exactly to write it? because there's error. it says "unexpected token"
Yes this the way export function tampildata() {} export method and import method as import { tampildata } from 'barang';.
Please see the updated answer that will helps you! Make sure you have presets: ['es2015', 'react', 'babel-preset-stage-0'] in your webpack configuration.
how i configure webpack?
Here is the good source to go ccoenraets.github.io/es6-tutorial-react/setup

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.